NonStop S-Series Server Description Manual (G06.24+)
TNS Execution Modes
HP NonStop S-Series Server Description Manual—520331-003
6-58
Returning a Value to the Caller
Returning a Value to the Caller
A function procedure can return a value back to its caller using the top of the register
stack. This, like parameter passing, requires coordination between the caller and the
called procedure. That is, the calling procedure must know the number of words
constituting the return value.
This topic and the following one describe an example of a procedure, named “f”, that
returns a value, and the instructions used to do so. This topic describes the procedure
that will be called, as illustrated in Figure 6-32. The following topic describes the
calling of this procedure and how the caller retrieves the returned value.
The procedure for this example returns the square of a number, “x”. The procedure is
written in TAL as follows:
INT PROC f (x);
INT x;
BEGIN
RETURN x * x;
END;
The instructions that perform this procedure are:
LOAD L -003 ! parameter x is obtained from L-003
LOAD L -003 ! load another copy of x
IMPY ! squared result now exists in R[0]
EXIT 4 ! delete stack marker and parameter x
The procedure begins executing with RP=7 (empty). The first LOAD instruction loads
the parameter “x” onto the top of the register stack. The procedure assumes that the
caller has placed the parameter in location L[–3] (that is, immediately preceding the
stack marker). The figure assumes that the passed value is 5.
Following this first LOAD, the RP setting is 0. The second LOAD again loads the
parameter “x”, because this is a procedure to square a number. Following this second
load, the RP setting is 1.
The IMPY instruction multiplies the values in the register stack. The instruction
eliminates both operands and leaves the result of the multiplication in R[0]. Following
this operation, the RP setting is 0.
The EXIT instruction causes a return to the caller, deleting the parameter and stack
marker (1 + 3 = 4) from the data stack. All that is left is the squared value on the top of
the register stack, with RP=0.
Now proceed to the next topic for a complete sequence of passing a parameter, calling
this procedure, retrieving the returned value, and using it in the calling procedure.