User guide

2.4. DECLARATIONS 29
where N is a name and K is a manifest constant expression. A location is
allocated for N and initialized to a vector whose lower bound is 0 and wh os e
upper bound is K. The variable N and the vector elements ( N !0 to N !K) reside
in the runtime stack and only continue to exist while control remains within the
scope of the declaration.
2.4.8 Function and Routine Declarations
These declaration s have the following fo rm:
LET N ( N
1
,..., N
n
) = E
LET N ( N
1
,..., N
n
) BE C
where N is the name of the function or routine being declared, N
1
,...,N
n
are
its formal parameters. A function is defined using = and returns E as result. A
routine is defined using BE and executes the command C without returning a
result.
Some example declarations are as follows:
LET wrpn(n) BE { IF n>9 DO wrpn(n/10)
wrch(n REM 10 + ’0’)
}
LET gray(n) = n XOR n>>1
LET next() = VALOF { c := c+1
RESULTIS !c
}
If a fu nction or routine is declared in the scope of a global variable with the
same name then the global variable is given an i n i t ia l value representing that
function or routine (see section 2.5).
A function is defined using equals (=) and has a result wh en called, while a
routine is defined using BE and has an undefined result. If a function is invo ked
as a rou t ine its result in thrown away, and if a routine is invoked as a fu n ct i on its
result is undefined. Functions and routines are otherwise sim i la r . See section 2.2.3
for information about the syntax of function and routine calls.
The arguments of a functio n s and routines b eh ave like named elements of a
dynamic vector and so exist only for the lifetime of the call. This vector h as as
many elements as there are formal parameters and they r ecei ve their initial val u es
from the actual parameters of the call. Functions and routines are variadic; that
is, the number of actual parameters need not equal the number of forma ls . If
there are to o few actual parameters, the missing ones are left uninitialized, and
if there are too many actual parameters, the extra ones are evaluated and then
discarded. Notice that arguments can be accessed by the expressions (@x)!0,