/*
 * File: functions.txt
 * -------------------
 */

/* Functions are assigned to variables using function. */
helloWorld = function() {
    wln("Hello world!");
};

/* Call helloWorld. */
helloWorld();

/* Create a new function that takes two parameters and returns their sum. */
sum = function(a, b) {
    return a + b;
};
wln(tostring(sum(3, 7)));


/* When calling a function, two reference variables are always created: this
   and self. self can be used for object-based designed and is explained in 
   another example. this is a reference to the function itself. */
countDown = function(n) {
    wln(tostring(n));
    if (n > 1) {
        sleep(1000);
        /* Make a recursive call through this. Actually we couldn't call
           countDown for this purpose here, because function can't normally
           access variables from the main program. */
        this(n - 1);
    }
};
countDown(5);

/* If the function variable has sub-variables you can access those through
   this. */
name.first = "John";
name.last = "Doe";
name = function() {
    return this.first + " " + this.last;
};
wln(name());

