Functions

fn main() { println(add(77, 33)) println(sub(100, 50)) } fn add(x int, y int) int { return x + y } fn sub(x int, y int) int { return x - y }

Again, the type comes after the argument's name.

When a function signature spans multiple lines, commas between parameters are optional:

fn greet( salutation string name string ) string { return 'Hey, ${salutation} ${name}!' }

Just like in Go and C, functions cannot be overloaded. This simplifies the code and improves maintainability and readability.

Hoisting

Functions can be used before their declaration: add and sub are declared after main, but can still be called from main. This is true for all declarations in V and eliminates the need for header files or thinking about the order of files and declarations.

Returning multiple values

fn foo() (int, int) { return 2, 3 } a, b := foo() println(a) // 2 println(b) // 3 c, _ := foo() // ignore values using `_`

A call returning multiple values can supply consecutive parameters of a non-variadic function or method. It can appear before or after ordinary arguments, as long as the expanded values match the receiving parameters. The returning function is evaluated once.

fn pair() (int, int) { return 2, 3 } fn digits(a int, b int, c int) int { return a * 100 + b * 10 + c } assert digits(pair(), 4) == 234 assert digits(1, pair()) == 123