Unformatted text preview:

Executing Method CallsFrame for a call (also called “activation record”)Frame for a callFrames are placed on the call stack and removed when call is finishedExecution of procedure call L1: p (2+5, 9-1);Execution of procedure callSlide 7Evaluation of a function call max(5,3)Slide 9Slide 10Slide 11Slide 12Slide 131Executing Method CallsThis lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects). The rules we give work automatically for recursive methods.YOU MUST MEMORIZE THE RULES FOR EXECUTING A METHOD CALL AND BE ABLE TO EXECUTE A METHOD CALL YOURSELF.Readings:Weiss, Section 7.3.3, page 241-242, discusses this topic briefly but doesn’t say enough.2Frame for a call(also called “activation record”)public static void p(int p1, int p2) { int x; x= p1; ...}procedure call: L1: p (2+5, 9-1);local varsparametersmethod name scope box return addressframe for callx _______p2_______p1_______ p (explain later)after L13Frame for a callpublic static void p(int p1, int p2) { int x; x= p1; ...}procedure call: L1: p (2+5, 9-1);When method body is being executed, look in frame for local variables and parameters.local varsparametersframe for callx _______p2_______p1_______ p (explain later)after L14Frames are placed on the call stackand removed when call is finishedpublic static void p(int p1, int p2) { int x= 5; L2: proc(x, p1+p2); ...}public static void main (String[] pars) { L1: p (2+5, 9-1);}main p proccall stackstack: a list with twooperations:(1) push an element onto it;(2) pop an element off it.5Execution of procedure call L1: p (2+5, 9-1); 1. Evaluate the arguments and push them onto the call stack.2. Push locations for the rest of the frame for the call onto the stack.3. Put in frame: name of method, local variables, return address, and scope box (note that the arguments have already been assigned to the parameters).4. Execute method body --look in frame at top of stack for all variables.5. Pop the frame from the stack; continue exe-cuting at the return address in popped frame.maincall stack6Execution of procedure call 1. Evaluate the arguments and push them onto the call stack.2. Push locations for the rest of the frame for the call onto the call stack.3. Put in frame: method name, local variables, return address, and scope box (arguments have already been assigned to parameters).4. Execute method body --look in frame at top of stack for all variables.5. Pop the frame from the stack; continue exe-cuting at the return address in popped frame.Five-minute quiz on Tuesday, 11 Sept. You will have to write this sequence of instructions and follow it in executing a method call. Everyone should get 100 on the quiz!Memorize this sequence of instructions!!Practice executing the sequence yourself!!!7Executing some procedure callspublic class Example { public static void main (String[] pars) {L1: print(2); } // Print integers 0..n in reverse order // Precondition: 0 <= n public static int print(int n) { if (n == 0) { System.out.println(n); return; } // {n > 0} System.out.println(n); L2: print(n-1); }}We’ll execute this program on the blackboard8Evaluation of a function call max(5,3) Consider executingint b= max(3,5) + max(4,6);Two points: (0) A call like max(3,5) yields a value, which is used in place of the call. We have to change our execution rules to take this into account.(1) This statement has TWO calls in it, so we have to revise our notion of a “return address”. It’s not always the next statement. We won’t deal with this in detail but will just assume we understand how to do it.9Evaluation of a function call max(5,3) 1. Evaluate the arguments and push them onto the call stack.2. Push locations for the rest of the frame for the call onto the stack.3. Put in frame: name of method, local variables, return address, and scope box (note that the arguments have already been assigned to the parameters).4. Execute method body --look in frame at top of stack for all variables, until a statement return e; is to be executed.5. Evaluate e; replace the frame on the top of the stack by the value of e; continue executing at the return address in popped frame. (the value at the top of the stack will be used as the value of the function call and will be popped from stack when used)10Execute Some Callspublic class Example { // Test method fact public static void main (String[ ] pars) {L1: int x= fact(2); } // = !n (for n >= 0) public static int fact(int n) { if (n == 0) {return 1; } // {n > 0} return n * /* L2: */ fact(n-1); }}We’ll execute this program on the blackboard.11Snapshot of call stackjust before executing the body of fact for the third timex _______pars_ ?___ main (explain later) in systemn _ 2___ fact (explain later) after L1n _ 1___ fact (explain later) after L2n _ 0___ fact (explain later) after L212Snapshot of call stackjust after the call fact(n-1) with n = 1 is finishedx _______pars_ ?___ main (explain later) in systemn _ 2___ fact (explain later) after L1n _ 1___ fact (explain later) after L2 113Snapshot of call stackjust after evaluation of n * fact(n-1) with n = 1 is finishedx _______pars_ ?___ main (explain later) in systemn _ 2___ fact (explain later) after L1n _ 1___ fact (explain later) after


View Full Document

CORNELL CS 211 - Study References

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Study References
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study References and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study References 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?