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.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

1 Pointers and Strings Section 5.4, , Lecture 12.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
Stacks CISC181 Spring 2004 P. T. Conrad University of Delaware.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
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.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
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.
1 Workin’ with Pointas An exercise in destroying your computer.
Objective: Students will be able to: Declare and use variables Input integers.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
 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.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
CS 240 Computer Programming 1
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Pointers What is the data type of pointer variables?
Pointer* Review Jason Stredwick.
#define #include<iostream> using namespace std; #define GO
Introduction to Programming Using C
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Basic notes on pointers in C
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Variables with Memory Diagram
Chapter 5 Function Basics
Dynamic Memory A whole heap of fun….
Dynamic Memory Copy Challenge
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Code::Block vs Visual C++
Pass by Reference.
Dynamic Memory A whole heap of fun….
Pointers Lecture 1 Thu, Jan 15, 2004.
When a function is called...
Pointers and dynamic objects
Dynamic Memory Copy Challenge
List Iterator Implementation
Pointers & Functions.
The Stack.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Introduction to Algorithms and Programming COMP151
Presentation transcript:

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 *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main

Tracing through E01, question 9 – step 2 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main 7 k

Tracing through E01, question 9 – step 3 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 7 k mysteryFunction()

Tracing through E01, question 9 – step 4 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 7 k mysteryFunction() x The address of k is copied into x Since an address is a pointer, and a pointer is an address, x now points to k.

Tracing through E01, question 9 – step 5 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 7 k mysteryFunction() x result

Tracing through E01, question 9 – step 6 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 7 k mysteryFunction() x 7 result (*x) means dereference the pointer x to go to what it points to. That is, k. Since (*x) is really k, the value of k, which is 7, gets copied into result.

Tracing through E01, question 9 – step 7 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 7 k mysteryFunction() x 7 21 result result gets multiplied by 3, and 7 changes to 21

Tracing through E01, question 9 – step 8 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 7 21 k mysteryFunction() x 21 result Now, (*x) is on the left hand side. (*x) dereferences the pointer x, follows the arrow to what is pointed to, which is k. So (*x) on the left hand side, really means that we are assigning a new value to k.

Tracing through E01, question 9 – step 9 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 21 k mysteryFunction() x 21 result When we reach the end of the function, the call frame, and all of its local variables are popped off of the stack, and they go away.

Tracing through E01, question 9 – step 10 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int *x); int main() { int k = 7; mysteryFunction(&k); cout << k << endl; return 0; } void mysteryFunction(int *x) { int result; result = (*x); result = result * 3; (*x) = result; } main() 21 k We have returned from the function, and we print the answer, 21.