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 `_`
On this page