Functions with a Fixed Number of Arguments
When you define a function with arguments, you define variables with the argument names as variable names. The arguments will hold the values that are passed into the function, and may be used as normal variables inside the function.
Functions with a Variable Number of Arguments
If you need a function to take an arbitrary number of arguments, you can use a parameter array. This is done by including, as the last element in the argument list, an array that is defined with three periods (…), then the name of the array and then a typed array annotation:
JScript example:
function FunctionName(... parArray : type[]) { <statements> }JavaScript example:
function FunctionName(... parArray) { <statements> } parArray can then be used like any other array inside the function.
parArray.lengthreturns the number of arguments in this array.
To refer to each argument in this array, use indexing as in all other arrays: 0 for the first argument, 1 for the second, and so on:
parArray[index]This makes it possible to write extremely flexible functions, as we have seen examples of in the built-in arithmetic functions in Forsta Plus; Sum, Count, Average, Max and Min (go to Arithmetic Functions for more information).
Note: While the current JScript engine does not support the arguments array, the JavaScript engine does:
JavaScript example:
function ExampleArgumentsFunction() {
for (var i = 0; i < arguments.length; i++) {
Response.Write(arguments[i]);
}
}Code Masking Based on a Variable Number of Conditional Expressions
The following function can be used to create a code mask where the items to be included in the mask is determined by the results from a number of conditional expressions that are sent in as parameters. It does require some caution when used: It requires all answers to have numeric codes, and the position of the expression represents each code (1,2,3,…).
function Pattern(... parArray : Object [])
{
__var codes = parArray.length;
__var s = new Set();
__for(var i: int = 0; i<codes; i++)
__{
____var code = i+1;
____var result = parArray[i];
____if(result==true)
____{
______s.add(code);
____}
__}
return s;
}
Here is an example of calling this function from a code mask:
Pattern(f("q1").toBoolean(),f("q2")=="2",f("q3").any("1","3","4"),true)If q1 is answered, the answer with code 1 would be included. If the answer to q2 is "2", the answer with code 2 would be included. If q3 (a multi) has answer(s) 1, 3 or 4, code 3 is included, and code 4 is always included.
Note: The script node defining this function must be placed before the question in which it is called in the questionnaire tree.