.he 'FUNCTION''Page %'
.fo 'Steven Hardy''January 78'
FUNCTION
.br
--------
.br
This word announces to the compiler that it is about to read a function definition
for example:
 	: FUNCTION SUMSQ (X,Y);
 	:	X*X + Y*Y
 	: END;
.br
The word "FUNCTION" is followed immediately by the name of the function being
defined and then by the formal parameters (names of
arguments) of the function - these being terminated by a semi-colon (as in
SUMSQ above) or by "=>" followed by output locals if there
are any.  (Commas and matching brackets are ignored between the function name and the semi-colon).
Then follows the body of the function (any number of POP11 commands)
followed by the word "END".  Notice that when a
function is defined it is not executed.  That only happens when it is called.
Only then are the commands in the body obeyed.  When
the function is called (say by executing SUMSQ(3,4)=> ) the formal parameters
(i.e. X and Y) are bound to the actual parameters
(i.e. 3 and 4) and the body of the function (i.e. X*X + Y*Y) executed thus
putting the results of the function (i.e. 25) on the
stack.
A function need not have any formal parameters, for example:
.tp 3
 	: FUNCTION GREET();
 	:     PR("HELLO")
 	: END;
.br
.el
Another example of a function of no arguments
is ITEMREAD,
which produces its result by reading an item from the input stream;  PR prints
its single argument on the output stream and
produces no result: i.e. it does not put anything on the stack.
Most functions need to use some variables - SUMSQ, above, uses X and
Y;  COUNT, shown earlier, uses LIST and TOTAL.  

.tp 6
One might expect this to cause problems, if, say SUMSQ was called by a function
already using X and Y, for example:
 	: FUNCTION SILLY(X,Y);
 	:	[%SUMSQ(6,8),X,Y%]
 	: END;
 	: SILLY(3,4)=>
.br
When control reaches the evaluation of X and Y in the decorated list in
SILLY, what will be the value of X and
Y?  SILLY set them to 3 and 4 respectively, but SUMSQ also used them,
set to 6 and 8.

It would be both inconvenient and confusing if the values of X and Y
were left at 6 and 8 because of the call of SUMSQ
and, as one might imagine, the POP11 system takes steps to prevent this happening.
When entering a function the POP11 system
remembers the values of the function's local variables
(i.e. it's formal parameter, output locals and other variables declared within
the function) and when the function completes
execution it resets all local variables to their earlier value.  This means
that the statement [%SUMSQ(6,8), X, Y%] evaluates to [100 3 4]
as X and Y will have been reset to 3 and 4 after SUMSQ has finished
with them.

Thus one need not worry about using the same variable name in two different
functions, provided the variable is local to each.
An implication of this is that a function may call itself:-
 	: FUNCTION COUNT (LIST);
 	:	IF	LIST = []
 	:	THEN	0
 	:	ELSE	COUNT(TL(LIST))+HD(LIST)
 	:	CLOSE
 	: END;
 	: COUNT ([3 9 7 12]) =>
 	** 31
.br
(See how much neater this recursive definition is than the iterative
definition in the entry for "=>".)
The words OPERATION and MACRO (described later) can also be used to
define functions - but with different conventions for execution.
