Syntax
function name(argument-list, . . .) {BLOCK}
Parameters
Parameter
|
Definition
|
---|---|
argument-list | The argument list can be a NULL entry if no variables are passed to the function, a single argument, or several arguments separated by commas. |
BLOCK | One or more MSL statements that define the action the function performs. |
name | Character label that is used to identify and call the function from within the MSL program. The name cannot be the same as a MSL built-in function or a MSL variable. |
Description
The function statement provides user-defined functions within MSL, similar to those available in the C programming language. The function keyword is required in a user function definition.
Functions must be defined before their first use, and the correct argument list must be passed in a function call. A function call always returns a character that represents a character string or numeric value. (All data types are represented within PSL as character strings.)
Arguments are passed by value to parameters (that is, local copies are created from the arguments’ data passed in), and thus changing a parameter will not affect the value of the argument. Function parameters are local to a function and can be named the same as global variables (or the same as parameters of other functions).
If a function definition appears in the middle of executable statements and control flow reaches that definition from above, the definition is skipped as a comment is skipped. The only way to enter the body of a function is to explicitly call it. The function definitions serve merely to define a function and are not invoked until called. Hence, it is possible to place executable code above, below, and between function definitions.
Example
function max(x,y){ if(x > y) { return x } else { return y } } m = max(1,2) print("maximum is ", m, "\n")
Of course, you could also do
function max(x,y) { return (x > y) ? x : y } m = max(1,2) print("maximum is ", m, "\n")