Java Reference
In-Depth Information
been created and the arguments have been stored in the parameters. When this
call is completed, the frame is erased, and the situation is as shown in the third
diagram again, but with the program counter changed to 2 to indicate that the
statement 2 of the method body is to be executed next.
The active frame , the frame for the call, whose body is being executed, is
always at the top of the stack. The frames below the top one are inactive .
Placing argument values on the call stack
Above, we simply said that the argument values are assigned to the param-
eters. But how is this done? The arguments are evaluated before the frame for the
call is created, and the calling side, the method body where the call is made,
knows nothing about how big the frame for the call will be.
Here is how it works. The argument values are pushed onto the call stack,
simply as values. When the frame is created, the locations containing these val-
ues become the parameters. Thus, the call stack itself acts as the communication
device for argument values.
Figure 2.12 illustrates this “parameter passing” mechanism. It shows the call
stack just before the first call to procedure printLarger2 , after the arguments
have been placed on the call stack, and then again after the frame has been cre-
ated. Notice that in the middle picture, the values are not yet named.
The return value of a function
The call stack is also used for communicating the value of a function to the
caller. When a statement return e; is executed, the active frame is popped from
the call stack and the value of e is pushed onto the call stack. At the place where
the function was called, the value is popped from the call stack and used as the
value of the function call.
printLarger: 1
X
c 6
active frame
b 20
printLarger2: 1
printLarger2: 1
X
X
c 6
c 6
active frame
b 20
b 20
m: 8
?
m: 8
m: 8
?
?
active frame
call stack
call stack
call stack
Figure 2.11
The call stack: the stack of frames for uncompleted calls
 
Search WWH ::




Custom Search