Functions

TIScript supports named and anonymous (lambda) functions.

Named functions

Named functions are declared using following syntax:

//...function body
 
Where:

  • <function-name> is a function name, either:
    • simple <nmtoken> , or
    • compound name: <nmtoken1> ['.' <nmtoken2> ['.' <nmtoken3> ['.' … <nmtokenN> ]]] 1)
  • <parameters> is a list of formal parameters of the function:
    [<parameter-name1> [, <parameter-name2> [, ... <parameter-nameN> ]]]

Anonymous functions

Anonymous function can be declared in place using following forms:

  1. classic JavaScript form:
     
  2. single expression lambda function form:
     
  3. 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:

 
Such function can be called with either two:
// r1 will be equal to 15 here
or three actual parameters:
// 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:

 
In runtime variable rest will contain array with actual parameters passed into the function. So after
 
r1 will contain value 10, and after
 
r2 will contain value 3

1) Declaration of function with compound name is a short form the following
name1.name2.name3. … .nameN = function( <parameters> ) { <statements> }
tiscript/functions.txt · Last modified: 2007/09/21 22:40 (external edit)
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0