Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functions

Declare a function at file scope with a name, typed parameters, and a return type. The available type names are Int, Bool, and Str.

fib(n: Int) -> Int {
    if n <= 1 {
        <- n;
    }

    <- fib(n - 1) + fib(n - 2);
}

print(fib(30));

This file prints 832040. Top-level statements run in source order, and a function body runs when called. A call can refer to a function declared later in the file, or to the function currently running.

Each call gets its own parameters and local bindings. A function can call other functions in the file. Bindings declared outside the function are outside its scope.

The compiler checks argument counts and types. Every path through a function must return a value of its declared type using <- expression;. A return inside a function resumes its caller. At file scope, a return ends execution and gives the host a value.

A function call can be used as an expression or written as a statement with a semicolon. The statement form discards the returned value.