10.3 Implementing Subprograms with Stack Dynamic Local Variables 453
ARI = activation record instance
n
Functional value
Parameter
Dynamic link
Return (to main)
Local
First ARI
for factorial
ARI
for main
3
?
?
n
Functional value
Parameter
Dynamic link
Return (to factorial)
Second ARI
for factorial
2
?
n
Top
At position 2
in factorial
value
ARI Local
for main^6
Top
In position 3
in main
value
Functional value
Parameter
Dynamic link
Return (to factorial)
Third ARI
for factorial
1
1
third call completed
Functional value Top
Parameter
Dynamic link
Return (to main)
Local
First ARI
for factorial
ARI
for main
(^3) n
6
? value
At position 2
in factorial
first call completed final results
n
Functional value
Parameter
Dynamic link
Return (to main)
Local
First ARI
for factorial
ARI
for main
3
?
?
n
Functional value Top
Parameter
Dynamic link
Return (to factorial)
Second ARI
for factorial
2
2
At position 2
in factorial
value
second call completed
Figure 10.8
Stack contents during execution of main and factorial
of factorial to be multiplied by its parameter value for n, which is 3,
yielding the final functional value of 6, which is then returned to the first
call to factorial in main.