
A6.6 External declarations 135
a2 = 1; /* a2 may be changed */
/* ... */
retp(a2,b1);
endp;
}
{x1, x2} = test2(2,3);
The example shows that external functions need not be declared before they are called
(although it would be good programming practice): if test1 is not found at the link
stage, an error will be reported.
All functions may have a return value, but this return value need not be used by the
caller. If a function does not return a value, its actual return value is undefined. Use
call to call a function and discard the return values. A function has only one return
value when the number of returns is left unspecified.
If a function is redefined, it automatically replaces the function which existed earlier
(without printing a warning).
The local statement allocates a local variable. If the local variable has the same
name as a global variable, the local will hide the global variable. Multiple declarations
result in a warning, unless it is a type change (such as from a matrix to a function, see
the genfunc example below).
The retp statement returns one or more values from the function, and also exits the
function. So, when the program flow reaches a retp statement, control returns to the
caller, without executing the remainder of the function. If a function fa returns p values,
and is in a call function fb, then the return from fa counts for p arguments in the call to
fb.
A fn function is a function with one return value. So the following two are equiva-
lent:
fn func(arg) = expr;
proc(1) func(arg); retp(expr); endp;
A keyword function differs from a proc in two ways: there is no return value, and
only one argument which is always a string. When a keyword is called, the argument
text up to the semicolon is passed as one string to the keyword function, unless the
argument starts with a ^, in which case it is interpreted as a variable name.
Functions can be passed as arguments, and an array of functions can be created. As
an example of the first:
proc(0)= func(a);
print("\nfunc:", a);
endp;
proc(0)= func3(&fnc); /* takes a function as argument */
local fnc:proc; /* tell compiler about this */
print("\nin func3:");
call fnc(100); /* and call the passed function */
endp;
call func3(&func); /* call func3 with func as argument */
And an example of an array of functions:
Komentarze do niniejszej Instrukcji