IBM AS/400 Frozen Dessert Maker User Manual


 
Program/Procedure Call Overview
Recursive Calls
Recursive calls are only allowed for subprocedures. A recursive call is one where
procedure A calls itself or calls procedure B which then calls procedure A again.
Each recursive call causes a new invocation of the procedure to be placed on the
call stack. The new invocation has new storage for all data items in automatic
storage, and that storage is unavailable to other invocations because it is local. (A
data item that is defined in a subprocedure uses automatic storage unless the
STATIC keyword is specified for the definition.) Note also that the automatic
storage that is associated with earlier invocations is unaffected by later invocations.
A main procedure that is on the call stack cannot be called until it returns to its
caller. Therefore, be careful not to call a procedure that might call an already active
main procedure.
Try to avoid situations that might inadvertently lead to recursive calls. For example,
suppose there are three modules, as shown in Figure 59.
main proc. X
main proc. Y
CALLP prc_B
CALLP prc_C
CALLP prc_A
MODULE X
MODULE Y MODULE Z
NOMAIN
PRC_A
PRC_B
PRC_D
PRC_C
Figure 59. Three Modules, each with subprocedures
You are running a program where procedure A in module X calls procedure B in
module Y. You are not aware of what procedure B does except that it processes
some fields. Procedure B in turn calls procedure C, which in turn calls procedure A.
Once procedure C calls procedure A, a recursive call has been made. The call
stack sequence is shown in Figure 60 on page 131. Note that the most recent call
stack entry is at the bottom.
130 ILE RPG for AS/400 Programmer's Guide