function

Defines a function as a list of statements that may be invoked by other parts of the code. The function may take zero or more arguments.

A function may optionally return a value using the return statement. In addition to giving the return value, the return statement also aborts execution of any following statements inside the function.

Variables that are set or changed inside the function will also be changed outside the function, unless the assignment is prefixed with the local keyword. Function arguments are implicitely local. Note, however, that arrays are passed by reference, so any changes done to arrays that are passed as arguments will be visible after calls to the function.

Syntax

    function function-name(variable-list)
        block-of-code
    endfunc;

The variable-list is a comma-separated list of variable names. The list may be empty if the function doesn't take any arguments.

Example

    function sayHello(name)
        println("Hello, ", name);
    endfunc;

    sayHello("boss");

or

    function addPercentage(value, percentage)
        local toAdd = value * percentage / 100.0;
        return value + toAdd;
    endfunc;

    vatPercentage = 25.0;
    priceWithoutVat = 50.0;
    toPay = addPercentage(priceWithoutVat, vatPercentage);