How Functions Work Part 2

Slides:



Advertisements
Similar presentations
For(int i = 1; i
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
 Review structures  Program to demonstrate a structure containing a pointer.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
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.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Example 21 #include<iostream.h> int main() { char Letter = 0;
Popping Items Off a Stack Using a Function Lesson xx
Cinda Heeren / Geoffrey Tien
Pointers & Arrays.
Pointers and Pointer-Based Strings
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers Psst… over there.
Pointers Psst… over there.
Pointer Basics Psst… over there.
Variables with Memory Diagram
Anatomy of a Function Part 2
Computer Organization & Compilation Process
Exceptions with Functions
Data Representation Bits
Manipulators CSCE 121 J. Michael Moore
Array & Pointers CSCE 121 J. Michael Moore.
Dynamic Memory A whole heap of fun….
Dynamic Memory Copy Challenge
Counting Loops.
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
How Functions Work Part 1
Input Validation CSCE 121 J. Michael Moore
Functions Pass By Value Pass by Reference
Static in Classes CSCE 121 J. Michael Moore.
How Dynamic Memory Works with Memory Diagram
How Classes Work with Memory Diagram
Dynamic Memory A whole heap of fun….
Pointers Lecture 1 Thu, Jan 15, 2004.
Observing how the machine acts
Function “Inputs and Outputs”
Destructor CSCE 121 J. Michael Moore.
CHAPTER 2 Arrays and Vectors.
Pointers and Pointer-Based Strings
Pointers & Arrays.
CHAPTER 2 Arrays and Vectors.
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Dynamic Memory Copy Challenge
Pointers & Functions.
The Stack.
Pointer Basics Psst… over there.
Computer Organization & Compilation Process
Programming Strings.
Input Validation CSCE 121 Based on slides created by Carlos Soto.
How Dynamic Memory Works with Memory Diagram
Dynamic Memory CSCE 121.
How Memory Leaks Work with Memory Diagram
How Classes Work with Memory Diagram
Presentation transcript:

How Functions Work Part 2 CSCE 121 J. Michael Moore

Memory Diagram How do references affect the memory diagram. References store an address. Rather than worry about a particular number: Use a small circle to indicate the value is an address. Use an arrow from the circle that points to the location/address.

Program int mi(int j) { int i = 5; return j % i; } int re(int& s, int p) { s = 12; return mi(s*p); int doe(int w) { int k = 2; int q = w+3; cout << "k: " << k << endl; int z = re(k, q); return z + w; } int main() { int b = doe(11); cout << "b: " << b << endl;

main output identifier stack int main() { int b = doe(11); cout << "b: " << b << endl; } identifier stack

main main output b identifier stack int main() { int b = doe(11); cout << "b: " << b << endl; } main b identifier stack

doe doe main output k: 2 z q 14 k 2 w 11 b identifier stack int doe(int w) { int k = 2; int q = w+3; cout << "k: " << k << endl; int z = re(k, q); return z + w; } z q 14 doe k 2 w 11 main b identifier stack

re re doe main output k: 2 p 14 s z q 14 k 2 12 w 11 b identifier int re(int& s, int p) { s = 12; return mi(s*p); } p 14 re s z q 14 doe k 2 12 w 11 main b identifier stack

mi mi re doe main output k: 2 i 5 j 168 3 p 2 s z q 14 k 2 12 w 11 b int mi(int j) { int i = 5; return j % i; } i 5 mi j 168 3 p 2 re s z q 14 doe k 2 12 w 11 main b identifier stack

re mi re doe main output k: 2 i 7 j 24 3 p 2 s 3 z q 14 k 2 12 w 11 b int re(int& s, int p) { s = 12; return mi(s*p); } i 7 mi j 24 3 p 2 re s 3 z q 14 doe k 2 12 w 11 main b identifier stack

doe mi re doe main output k: 2 k: 12 i 7 j 24 3 p 2 s 3 z 3 q 14 k 2 int doe(int w) { int k = 2; int q = w+3; cout << "k: " << k << endl; int z = re(k, q); return z + w; } i 7 mi j 24 3 p 2 re s 3 z 3 q 14 doe k 2 12 14 w 11 main b identifier stack

main mi re doe main output k: 2 k: 12 b: 14 i 7 j 24 3 p 2 s 3 z 3 q int main() { int b = doe(11); cout << "b: " << b << endl; } i 7 mi j 24 3 p 2 re s 3 z 3 q 14 doe k 2 12 14 w 11 main b 14 identifier stack