How Functions Work Part 1

Slides:



Advertisements
Similar presentations
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Advertisements

Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CPS120: Introduction to Computer Science Functions.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
C++ Programming Lecture 12 Functions – Part IV
User-Defined Functions (cont’d) - Reference Parameters.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Instructions for test_function
User-Written Functions
Two-Dimensional Arrays Lesson xx
CS41B recursion David Kauchak CS 52 – Fall 2015.
Dynamic Memory CSCE 121 J. Michael Moore.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
void Pointers Lesson xx
Arrays & Functions Lesson xx
Variables with Memory Diagram
Anatomy of a Function Part 2
Computer Organization & Compilation Process
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
Exceptions with Functions
Popping Items Off a Stack Lesson xx
IO Overview CSCE 121 J. Michael Moore
Manipulators CSCE 121 J. Michael Moore
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.
Array & Pointers CSCE 121 J. Michael Moore.
Dynamic Memory Copy Challenge
Return by Reference CSCE 121 J. Michael Moore.
Functions Overview CSCE 121 J. Michael Moore
Anatomy of a Function Part 1
Anatomy of a Function Part 3
Exceptions CSCE 121 J. Michael Moore
Debugging CSCE 121 J. Michael Moore.
Functions Pass By Value Pass by Reference
How Classes Work with Memory Diagram
How Functions Work Part 2
Function Overloading CSCE 121 J. Michael Moore
Simulating Reference Parameters in C
Function “Inputs and Outputs”
Destructor CSCE 121 J. Michael Moore.
Guidelines for Writing Functions
CS150 Introduction to Computer Science 1
Pointers and dynamic objects
Dynamic Memory Copy Challenge
Computer Organization & Compilation Process
Anatomy of a Function Part 1
References Revisted (Ch 5)
Dynamic Memory CSCE 121.
How Memory Leaks Work with Memory Diagram
Introduction to Methods and Interfaces
Presentation transcript:

How Functions Work Part 1 CSCE 121 J. Michael Moore

Stack Frames and Function Calls Function Called (including main) New area of memory (stack frame) is set up on top of the stack. Stack frame has an entry for each formal parameter each local variable Body of function executes Function finishes and its stack frame goes away i.e. memory is recycled for later use Potential source of programming bugs if you don’t understand how this works! Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch

Stack Frame Contains Parameters Local variables Value of actual argument is copied into corresponding formal argument variable in stack frame Order matches the order in both argument lists: 1st to 1st, 2nd to 2nd, etc. So the function computes using the formal argument No change is made to the actual argument Local variables Information for returning to the calling function E.g. address in code of calling function to go back to

Memory Diagram Helps us visualize the call stack zyBooks starts at top since addressing is considered to start at zero and frequently drawn with zero at the top. However, when talking about stacking, we put things on top! So we will start at the bottom so that when we put a stack frame on top, it looks like it is going on top. When we draw a stack frame, we’ll include information about Parameters Local variables Name of function

Program output identifier stack int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; identifier stack

Program main output b identifier stack int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; main b identifier stack

output Program int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; Notice that actual argument values are copied into the variables set up for the formal arguments. whoop 10 k 3 em b 6 a 1 main b identifier stack

Program em main output whoop 10 k 3 b 6 10 a 1 b identifier stack int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; whoop 10 k 3 em b 6 10 a 1 main b identifier stack

Program gig main output int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; However, when sketching diagrams, we do not want to have to erase! howdy gig 4 rev 10 b main identifier stack

Program em main output Instead of erasing, just put an X over the int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; Instead of erasing, just put an X over the deleted stack frame. whoop 10 k 3 em b 6 10 a 1 main b identifier stack

Program gig em main output howdy 4 rev 10 whoop 10 k 3 b 6 10 a 1 b int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; howdy gig 4 rev 10 whoop 10 k 3 em b 6 10 a 1 main b identifier stack

Program em gig em main output whoop 17 k 3 b 4 a 10 howdy 4 rev 10 int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; k 3 em b 4 a 10 howdy gig 4 rev 10 whoop 10 k 3 em b 6 10 a 1 main b identifier stack

Program em gig em main output whoop 17 k 3 b 4 17 a 10 howdy 4 rev 10 int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; k 3 em b 4 17 a 10 howdy gig 4 rev 10 whoop 10 k 3 em b 6 10 a 1 main b identifier stack

Program em gig em main output whoop 17 k 3 b 4 17 a 10 howdy 4 rev 10 int em(int a, int b) { int k = 3; int whoop = a + b + k; return whoop; } int gig(int rev) { int howdy = 4; return em(rev, howdy); int main() { int b = gig(em(1, 6)); cout << "b: " << b << endl; k 3 em b 4 17 a 10 howdy gig 4 rev 10 whoop 10 17 k 3 em b 6 10 a 1 main b 17 identifier stack

Program em gig em main output b: 17 whoop 17 k 3 b 4 We will expand on this model as the semester progresses. 17 a 10 howdy gig 4 rev 10 whoop 10 17 k 3 memory diagrams You will see on the exam em b 6 10 a 1 main b 17 identifier stack