Function Features
Multiple return values
Section titled “Multiple return values”do swap(a i64, b i64) -> (i64, i64) { return b, a}
do main() { mut x, y = swap(1, 2) println("${x} ${y}")}Default and named arguments
Section titled “Default and named arguments”Parameters with defaults must come after required ones. Named arguments can appear in any order, after any positional ones.
do connect(host string, port i64 = 8080, verbose bool = false) { if verbose { println("Connecting to ${host}:${port}") }}
do main() { connect("localhost", verbose: true) connect(port: 3000, host: "example.com", verbose: true)}Mutable parameters
Section titled “Mutable parameters”A & before the parameter name lets the function modify the caller’s mut variable. The call site passes the variable normally.
do increment(&x i64) { x = x + 1}
do main() { mut value i64 = 5 increment(value) println(value)}Function references
Section titled “Function references”()name or ref(name) makes a reference to a function. It must be bound to a const. Take one as a parameter with a func type.
do double(n i64) -> i64 { return n * 2}
do apply(x i64, f func(i64) -> i64) -> i64 { return f(x)}
do main() { const twice = ()double println(twice(4)) println(apply(5, ()double)) println(apply(6, ref(double)))}