HP Pascal/iX Reference Manual (31502-90022)

8-: 6
FORWARD;
.
.
FUNCTION exclusive_or; { Parameters not repeated. }
BEGIN
exclusive_or:= (x AND NOT y) OR (NOT x AND y);
END;
Recursion
A
recursive
procedure or function is a procedure or function that calls
itself. It is also legal for procedure A to call procedure B that in
turn calls procedure A. This is
indirect recursion
and is often an
instance when the FORWARD directive is useful. Note that when a routine
is called recursively, new local variables are created for each
invocation of the routine.
Example
FUNCTION factorial (n: integer): integer;
{ Calculates factorial recursively }
BEGIN
IF n = 0 THEN
factorial := 1
ELSE
factorial := n * factorial(n-1);
END;
Function Calls
A
function call
invokes the block of a standard or declared function and
returns a value to the calling point of the program. Because an operator
can perform some action on this value, a function result is an
expression.
A
function call
consists of a function identifier and an optional list of
actual parameters in parentheses. The actual parameters must match the
formal parameters in
number, type,
and
order
. The function result has
the type specified in the function heading. Actual value parameters are
expressions that must be assignment compatible with the formal value
parameters or, in the case of value conformant array parameters, conform
with the conformant array schema.
Actual
reference parameters
are variables that must be type identical
with the formal variable parameters or, in the case of variable
conformant array parameters, conform with the conformant array schema.
Components of a packed structure may not appear as actual variable
parameters.
Actual
procedural
or
functional parameters
are the names of declared
procedures or functions. Standard functions or procedures are not legal
actual parameters.
The
parameter list
, if any, of an actual procedural or functional
parameter, must be congruent with the parameter list of the formal
procedural or functional parameter. For more information, see the
section on Procedures in this chapter.
Functions may call themselves recursively. Refer to "Recursion"
earlier in this chapter for more details.
If an actual functional or procedural parameter, upon invocation,
accesses any entity non-locally, then the entity accessed is one that is
accessible to the function or procedure when its identifier is passed.
For example, suppose Procedure A uses the non-local variable x. If A is
passed as a parameter to Function B, then it still has access to x, even
if x is otherwise inaccessible in B.
If the
function result
is a structured type, then the function call may
select a particular component as the result. This requires the use of an
appropriate selector.