Functions
Functions are ubiquitous in TL Script. A function is a script expression that requires one or more inputs to be calculated. Most scripts used to parameterize user interface components in TopLogic must be written as functions. The script usually expects an input (argument) from the surrounding component. In addition to user-defined functions, TL-Script offers a whole range of predefined functions.
A function consists of the declaration of a parameter, which names the expected input and an expression, which refers to this parameter:
x -> someExpression($x)
The above script defines a function that expects an argument x
and generates a value by evaluating the expression someExpression($x)
. Here, someExpression($x)
is representative of any TL script expression that uses the variable x
. In this expression the value of the function argument can be accessed with the syntax $x
(see Variables).
A function that expects multiple arguments is written as follows:
x -> y -> someExpression($x, $y)
Above, a function is defined with two parameters x
and y
.
Functions can also be stored in a variable to be called at a later time (see Function Application).
{
myFunc = x -> $x * 5;
$myFunc(10)
}
Above script defines a function that multiplies its argument by 5
and assigns this function to the variable myFunc
. Then this function is called with the value 10
and consequently returns the value 50
.
Examples
For information on calling functions, see Function Application.
Function with one parameter
x -> 5 * $x
Output: /
The input for x
is multiplied by 5.
Function with two parameters
x -> y -> $x * $y
Output: /
Multiplies the inputs of x
and y
together.
Function in one variable
{
multiply = x -> 5 * $x;
}
Output: /
The function for mulitplication is stored in the variable multiply
. This can be accessed with $multiply
to execute it with apply(), for example.