Skip to content

Functions

Rohansi edited this page Dec 12, 2014 · 16 revisions

Functions can be defined in many ways, here are a few ways to create the same function:

fun add(a, b) {
    return a + b;
}
var add = fun (a, b) {
    return a + b;
};
// or
var add = (a, b) -> {
    return a + b;
};
var add = fun (a, b) -> a + b;
// or
var add = (a, b) -> a + b;
fun add(a, b) -> a + b;

Variable Captures

Functions have access to all variables visible to their scope, even when called from different scopes (see closures).

Argument Lists

Sometimes you may want to create functions which accept a variable length of arguments. This can be done by prefixing the last argument with .... When the function is called, that argument will be an array of the values after the normal arguments.

fun add(a, b, ...nums) {
    var sum = a + b;
    foreach (var n in nums) {
        sum += n;
    }
    return sum;
}

Tail Calls

Mond supports basic tail call optimization on named functions.