Skip to content

Functions

Bill Hails edited this page Oct 21, 2018 · 10 revisions

Anonymous functions can be created with the fn keyword:

fn (x) { x * x };

Anonymous functions can be passed as variables, or immediately invoked as here:

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

You can of course name your functions with define:

define 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 and in fact is re-written to the above define form before evaluation.

Sub-Topics:

Up: Home

Next: Closure

Clone this wiki locally