Skip to content
Jing Lu edited this page May 22, 2013 · 21 revisions

Function is a one of object type that could be called by '(' and ')' paired operation.

Define

Function can be defined using following syntax:

function identifier(argument-list) {
    statements
}

where argument-list is

identifier[, identifier]*

See Language Specification.

For example:

function hello() {
    console.log('hello world!');
}

Call

Syntax to call a defined function:

hello()

Arguments

To pass arguments into function, the syntax of definition is:

function show(a, b) {
    console.log(a + b);
}

Then call this function with arguments:

show(10, 5);

The result is:

15

Function Override

The function override is not supported by ReoScript. However, you could simulate the same effect by using argument array. See Variable Number of Arguments below.

Variable Number of Arguments

ReoScript finds and calls a function by specified function name, even given arguments does not matched to that function. For example:

function plus(a, b, c) {
    return a + b + c;
}

Call this function:

var z = plus(1, 2, 3);        // 'z' is 6

This function can also be called with different number of arguments:

var z = plus(1, 2);           // calling is OK, but 'z' is NaN since 'c' is null

Arguments that not specified in calling statement will be set to null automatically. The a + b + c will returns NaN because c is null(+ operation does not work with numbers and null). To solve this issue, you could:

  1. Check arguments that be specified or not
  2. Use __args__ system variable

For example:

function plus(a, b, c) {
    return c == null ? (a + b) : (a + b + c);
}

Or you could use an argument array __args__, it is an array contains all of variables:

function plus(a, b, c) {
    return __args__.length == 3 ? (a + b + c) : (a + b);
}

Another example that joins all of arguments into one string using __args__.

function join() {
    var output = '';
    for (str in __args__) {
        if (output.length > 0) output += '-';
        output += str;
    }
    return output;
}

Use this function:

var result = join('The', 'Earth', 'and', 'Sun');

The result is:

The-Earth-and-Sun

Access Scope

Available to inner scope

Since function is also an object which be stored as variable or property to another object. The ruler of scope to access and call functions is same as Variables. All functions defined in outer scope can be called in inner scope. Functions can also defined in local call-scope. For example:

function outer() {
}

outer is global object(defined as property to global object) is available to all of call-scopes:

function fun_b() {
  outer();              // OK
}

Nested Function

It is possible to define a function inside another function, for example:

function outer() {
    function inner() {
    }
}

Actually it is same as:

function outer() {
    var inner = function() {
    };
}

Inner function can only be called inside inner scope:

function outer() {
    var inner = function() {
    };

    inner();               // OK
}

inner();                   // error: Function is not defined: 'inner' 

Notice that inner is not a property of outer:

outer.inner();             // error

Since outer.inner is null so you will get an error function outer() {...} has no method 'inner'. There is no way to call an inner function from outside scope, the inner function is only available to the scope itself.

Recursive

You could call a function in itself to implement recursive calling:

function findMore(path) {
    if (path.hasMore()) {
         return find_more(path.getMore());
    }
}

Anonymous Function

A function defined without a specified name is an anonymous function. For example:

var hello = function() { };

Anonymous function must be defined, assigned to a variable or property, or called immediately.

var hello = function() { };       // OK
function() { };                   // error: not is a valid expression-statement, it needs left-hand
function() { } ();                // OK: call an anonymous function immediately