Functions
A function is a let binding that takes parameters. Where a value binding is worked out once, a function runs every
time it's called:
let alwaysTen() = 10
let plusOne(value: i32) = value + 1
let add(left: i32, right: i32) = left + right
A longer function takes a block, and return gives its result:
let describe(count: i32) {
if (count == 0) {
return "none"
}
return "some"
}
Types
A parameter's type is always written. The return type is worked out from the body, or you can write it after the parameters:
let square(input: i32) : i32 {
return input * input
}
A function that calls itself must write its return type, since the compiler can't work it out from a body that depends on itself:
let factorial(n: i32) : i32 {
if (n <= 1) {
return 1
}
return n * factorial(n - 1)
}
Default values
A parameter can have a default, which makes it optional at the call. A default is a literal, and goes on the last parameters:
let repeat(text: string, count: i32 = 2) {
for (count times) {
println(text)
}
}
entrypoint main() {
repeat("twice")
repeat("three times", 3)
}
Functions inside functions
A function can declare its own helpers, which only it can see:
let perimeter(width: i32, height: i32) {
let double(value: i32) = value * 2
return double(width) + double(height)
}
Entry points
A program starts at a function marked entrypoint. Getting Started shows how.
Lambdas, functions written inline as values, are not in V0 yet.