====== Functions ======
TIScript supports named and anonymous (lambda) functions.
===== Named functions =====
Named functions are declared using following syntax:
function ( )
{
//...function body
}
Where:
* '''' is a function name, either:
* simple '''' , or
* compound name: '' ['.' ['.' ['.' ... ]]]'' ((Declaration of function with compound name is a short form the following\\ ''name1.name2.name3. ... .nameN = function( ) { }''))
* '''' is a list of formal parameters of the function:\\ [ [, [, ... ]]]
===== Anonymous functions =====
Anonymous function can be declared in place using following forms:
- classic JavaScript form:\\ function ( ) { }
- single expression lambda function form:\\ : :
- block lambda function form:\\ : { }
Nested functions (functions inside functions) are allowed.
===== Optional parameters =====
The language supports optional parameters in function declarations:
==== Parameters with default values ====
Some parameters in the function declaration may have default value defined, for example:
function Foo( a, b, c = 12)
{
return a + b + c;
}
Such function can be called with either two:
var r1 = Foo(1,2); // r1 will be equal to 15 here
or three actual parameters:
var r2 = Foo(1,2,3); // r2 will be equal to 6 here
Parameter with predefined value must not have non-optional parameter on its right in the list. In other words only last (or all) parameters can have default values.
==== Varargs ====
a.k.a. Parameter vectors
There are situations when you will need to define functions with number of parameters unknown upfront.
Such functions can be declared as:
function Bar(a,b,rest..)
{
var total = a + b;
for( var n in rest ) total += n;
return total;
}
In runtime variable ''rest'' will contain array with actual parameters passed into the function. So after
var r1 = Bar(1,2,3,4);
''r1'' will contain value ''10'', and after
var r2 = Bar(1,2);
''r2'' will contain value ''3''