Skip to content

Functions

Bill Hails edited this page Aug 9, 2024 · 3 revisions

Anonymous functions can be created with the fn keyword:

fn (x) { x * x };

Functions can be passed as variables, or immediately invoked as here:

fn (x) { x * x } (5); // 25

You can of course name your functions by assigning them:

factorial = fn (n) {
    if (n == 0) {
        1
    } else {
        n * factorial(n - 1)
    }
}

The fn keyword can take a function name, so:

fn factorial(n) {
    if (n == 0) {
        1
    } else {
        n * factorial(n - 1)
    }
}

is completely equivalent.

Next: Closure

Clone this wiki locally