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

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

Define

Function could be defined using following syntax:

function identifier(argument-list) {
    statements
}

where argument-list is

variable[, variable]*

See full Language Specification.

For example:

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

Call

Syntax to call a defined function:

hello()

Arguments

For passing arguments into function, the syntax of definition is:

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

Then call this function by passing arguments:

show(10, 5);

The result is:

15

Function Override

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

Variable Number of Arguments

ReoScript finds and calls a function by specified function name, even given arguments is 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 called __args__, it is an array contains all of variables what passed by calling statements:

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