160 likes | 268 Views
CS100A, Fall 1997. Lecture 10, Thursday, 2 October. Method calls We describe exactly how a method call is executed. You should learn these steps and be able to perform them yourselves, so memorize them and practice them.
E N D
CS100A, Fall 1997 Lecture 10, Thursday, 2 October. Method calls We describe exactly how a method call is executed. You should learn these steps and be able to perform them yourselves, so memorize them andpractice them. Much of the difficult some of you are having stems from lack of understanding about the precise execution of assignments, conditional statements, etc. These difficulties can be overcome by learning the steps in executing each of kind of statement and practicing executing these steps yourself. Read Lewis/Loftus, pp. 136-140. CS100A, Fall 1997. Lecture10
instance of class C c __4__ d __3__ b Example: To execute an assignment x= e; (0) Evaluate e (yielding a value v, say) (1) Store v in the variable described by x. b.c = x * b.c (v is: 4; store v in field c of b) b= new C(8,7); (v is the name of new instance of class C; store that name in b) d 7 CS100A, Fall 1997. Lecture 8
Execution of a method call (or method invocation) is performed as follows. We give more detail that we have previously. • 0. Allocate a new set of locations to contain the parameters and local variables of the method being called, in what we can call a frame. • 1. Assign the arguments of the call to the parameters of the method. • 2. Execute the procedure body. • 3. Delete the frame produced in step 0; if the method is a function, return the value of the function to the place of call. Lewis/Loftus teminology: parameter: formal parameter argument: actual parameter CS100A, Fall 1997. Lecture 8
Example of execution of a method call. // Yield the maximum of x and y publicstaticint max(int x, int y) {int z; z= x; if (y > x) z= y; return z; } The call is in the assignment given below. int b; b= max (3*20, 6+5) + 2; CS100A, Fall 1997. Lecture 8
y x b z z x y b b ? ? ? 60 12 7 7 ? 7 frame for the call frame for the call b= max (3*20, b+5) + 2; The state before execution, with b = 7: The state after execution of step 0. The state after execution of step 1. CS100A, Fall 1997. Lecture 8
x b x y z y x z y z b b 12 7 ? 60 7 7 60 12 60 60 12 60 frame for the call frame for the call frame for the call Performing step 2, initial state After execution of z= x; After execution of the conditional statement: CS100A, Fall 1997. Lecture 8
b b 7 62 Executing the return, which terminates execution of the call (perform step 3). 60 b= max (3*20, b+5) + 2; Memorize the steps in executing a procedure call, given on slide 3. Practise executing simple procedure calls yourself. CS100A, Fall 1997. Lecture 8
Coordinate Coordinate x b x d y y 5 1 3 4 // Add b to c publicstaticvoid addC(Coordinate b, Coordinate c) {c.x= c.x+b.x; c.y= c.y+b.y; } Execute the call addC(d,b); assuming the initial state is: CS100A, Fall 1997. Lecture 8
frame for call c b ? ? Coordinate Coordinate x b d x y y 5 1 3 4 Step 0: allocate a frame for the method Note that, below, there are two variable named b. There is no ambiguity. Later, when executing the method body, always use the variables in the frame for the call. CS100A, Fall 1997. Lecture 8
frame for call b c Coordinate Coordinate b x d x y y 1 5 4 3 Execution of step 1 yields: CS100A, Fall 1997. Lecture 8
frame for call b c Coordinate Coordinate b x d x y y 1 6 4 7 Execution of step 2 yields: CS100A, Fall 1997. Lecture 8
Coordinate Coordinate x b x d y y 6 1 7 4 Execution of step 3 yields: CS100A, Fall 1997. Lecture 8
Execution of new Coordinate and a constructor public class Coordinate {public int x; public int y; // Constructor: an instance with x= b and y=0 public Coordinate(int b) {x= b; y= 0;} } Coordinate d; d= new Coordinate(9); To evaluate new Coordinate(9): • 0. Create a new instance of Coordinate • 1. Execute the call Coordinate(7) CS100A, Fall 1997. Lecture 8
d ? Coordinate ? ? y x Coordinate (constructor) Initial state: Execution of step 0 yields the following. Note that we have placed method Coordinate within the instance. The instance contains all the fields and methods defined within the class. This will be important when executing the method body, as shown below, to follow the scope rules. CS100A, Fall 1997. Lecture 8
Coordinate Coordinate ? ? ? ? y y x x Coordinate (constructor) Coordinate (constructor) frame for call frame for call b b ? 9 Step 1: Execute the call on the constructor. Rule: when drawing the frame, place the frame within the class instance that contains the called method! CS100A, Fall 1997. Lecture 8
Coordinate 9 9 0 y x x Coordinate (constructor) frame for call b 9 After execution of constructor body After call is completed: d= new Coordinate(9); So the name of this instance is stored in d. Coordinate 0 y Coordinate (constructor) CS100A, Fall 1997. Lecture 8