up previous next
define

define a function

Syntax
Define F(X_1,...,X_n)   C   EndDefine
  return FUNCTION

Description
1. INTRODUCTION. This command adds the user-defined function F to the library. The function F can be called in the following way:
    F(E_1,...,E_n)
where the E_i 's are expressions. The result of the evaluation of each expression E_i is assigned to the respective formal parameter X_i , and the command sequence C is executed. If, during the execution of C , a statement Return E is executed, then the result of the evaluation of E is the return-value of the function F . If no Return command is executed, or Return is executed without argument, then the return-value is Null .

Example
/**/  Define square(X)
/**/    Return X^2;
/**/  EndDefine;

/**/  square(5);
25
2. SCOPE. Every variable defined or modified by the command sequence C is considered local to the function unless the variable is global or relative to a ref parameter. See TopLevel for the use of global variables. See ref to learn about calling a function by reference, i.e. so that the function can change the value of an existing variable.

Example
/**/  Define Example_1(L)
/**/    L := L + 5;
/**/    Return L;
/**/  EndDefine;

/**/  L := 0;
/**/  Example_1(L);
5
/**/  L;  -- L is unchanged despite the function call.
0
3. VARIABLE NUMBER OF PARAMETERS. It is also possible to have a variable number of parameters using the syntax
    Define F(...) C EndDefine;
In this case the special variable ARGV will contain the list of the arguments passed to the function.

Example
/**/  Define MySum(...)
/**/    If len(ARGV) = 0 Then Return 12345;
/**/    Else
/**/      ans := 0;
/**/      Foreach N In ARGV Do ans := ans+N; EndForeach;
/**/    EndIf;
/**/    Return ans;
/**/  EndDefine;

/**/  MySum(1,2,3,4,5);
15
/**/  MySum();
12345
The old statement, Help S; is OBSOLETE!
See Also