1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).

Slides:



Advertisements
Similar presentations
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Advertisements

1 Procedural Programming Paradigm Stacks and Procedures.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Procedures and Control Flow CS351 – Programming Paradigms.
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Runtime Environments Source language issues Storage organization
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
RECURSION Lecture 7 CS2110 – Spring Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Call Stacks John Keyser. Stacks A very basic data structure. – Data structure: a container for holding data in a program. – Classes, structs can be thought.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Runtime Environments Compiler Construction Chapter 7.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
CSC 8505 Compiler Construction Runtime Environments.
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Recursion CENG 707.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
Functions.
CS 326 Programming Languages, Concepts and Implementation
Method Parameters and Overloading
Programming Fundamentals Lecture #7 Functions
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Sit next to someone. Today, we do some work in pairs.
The University of Adelaide, School of Computer Science
10/4: Lecture Topics Overflow and underflow Logical operations
CS2011 Introduction to Programming I Methods (II)
Sit next to someone. Today, we do some work in pairs.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Lecture 12: Message passing The Environment Model
UNIT V Run Time Environments.
When a function is called...
Systems Architecture I
Scope, Function Calls and Storage Management
Corresponds with Chapter 5
Implementing Functions: Overview
Presentation transcript:

1 Executing Method Calls This 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 , discusses this topic briefly but doesn’t say enough.

2 Frame 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 vars parameters method name scope box return address frame for call x _______ p2_______ p1_______ p (explain later) after L1

3 Frame for a call public 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 vars parameters frame for call x _______ p2_______ p1_______ p (explain later) after L1

4 Frames are placed on the call stack and removed when call is finished public 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 proc call stack stack: a list with two operations: (1) push an element onto it; (2) pop an element off it.

5 Execution 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. main call stack

6 Execution 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!!!

7 Executing some procedure calls public 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 blackboard

8 Evaluation of a function call max(5,3) Consider executing int 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.

9 Evaluation 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)

10 Execute Some Calls public 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.

11 Snapshot of call stack just before executing the body of fact for the third time x _______ pars_ ?___ main (explain later) in system n _ 2___ fact (explain later) after L1 n _ 1___ fact (explain later) after L2 n _ 0___ fact (explain later) after L2

12 Snapshot of call stack just after the call fact(n-1) with n = 1 is finished x _______ pars_ ?___ main (explain later) in system n _ 2___ fact (explain later) after L1 n _ 1___ fact (explain later) after L2 1

13 Snapshot of call stack just after evaluation of n * fact(n-1) with n = 1 is finished x _______ pars_ ?___ main (explain later) in system n _ 2___ fact (explain later) after L1 n _ 1___ fact (explain later) after L2