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

A function is declared with the fun keyword, followed by its name, parameters, and an optional return type.

Basic Functions

fun square(n) {
    return n * n;
}

fun main() {
    result := square(5);
    print(result); // 25
}

Recursive Functions

Functions can also call themselves recursively. Just remember to include a base case! :D

fun foo(n) {
    print(n);

    if n > 0 {
        bar(n - 1);
    }
}

fun bar(n) {
    print(n);

    if n > 0 {
        foo(n - 1);
    }
}

fun main() {
    foo(10);
}