IBM AS/400 manual Recursive Calls, Program/Procedure Call Overview

Models: AS/400

1 489
Download 489 pages 47.95 Kb
Page 154
Image 154
Recursive Calls

Program/Procedure Call Overview

Recursive Calls

Recursive calls are only allowed

for

subproceduresrecursive call

.is A one where

 

procedure

A

calls

itself or

calls

procedure B which then calls proced

Each

recursive call

causes

a

new

invocation

of

the

procedure

to

be

call stack. The new invocation has

new storage for all data items in a

storage, and that storage is unavailable

to

other

 

invocations

because

data

item

that

is

defined

in

a

subprocedure

uses automatic

storage

STATIC keyword is specified for

the definition.) Note also that the au

storage

that

is

associated

with

earlier

invocations

is

unaffected

by

A main procedure that is on the

call stack cannot be called until it

caller.

Therefore,

be careful

not to

call

a

procedure

that

might

call

main

procedure.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Try to avoid situations that might inadvertently lead to recursive calls suppose there are three modules, as shown in Figure 59.

59. Three Modules, each with subprocedures MODULE XManual background

main proc. X

Manual background PRC_AManual background

CALLP prc_B

Manual background MODULE Y

main proc. Y

Manual background PRC_BManual background

CALLP prc_C

Manual background MODULE Z

NOMAIN

Manual background PRC_CManual background

CALLP prc_A

Manual background PRC_DManual background

Figure

59. Three Modules, each with subprocedures

 

 

 

You are

running

a

program where procedure A in

module

X calls

procedu

module

Y.

You

are not aware of what procedure B does except that

some

fields. Procedure B in turn calls procedure C, which in turn calls

Once

procedure C calls procedure A, a recursive call has been made.

stack

sequence

is

shown

in Figure 60 on page 131.

Note

that the

most r

stack

entry is

at

the

bottom.

 

 

 

130 ILE RPG for AS/400 Programmer's Guide

Page 154
Image 154
IBM AS/400 manual Recursive Calls, Program/Procedure Call Overview, Three Modules, each with subprocedures