first look at pointers

12
First Look at Pointers

Upload: emartinezromero

Post on 22-Nov-2014

636 views

Category:

Lifestyle


1 download

DESCRIPTION

Call by Reference, Call by Value

TRANSCRIPT

Page 1: First Look at Pointers

First Look at Pointers

Page 2: First Look at Pointers

Function Parameter Call by Value

When a function is invoked and the function takes parameters, how do those parameters relate to the original values in the calling function?

main (itself a function) has variables 1, 2 and 3.

Main calls Function A with parameters 1, 2, and 3, for which we fill in the values of variables 1, 2, and 3.

Function A goes about its business, including changing the parameters.

Then we return to main.

Page 3: First Look at Pointers

Main is a function

int main(){string msg = "do solution1";cout << msg << endl;

int variable1, variable2, variable3;

variable1 = 1;variable2 = 2;variable3 = 3;

cout << "variable1 [" << variable1 << "] variable2 [" << variable2 << "] variable3 [" << variable3 << "]" << endl;

functionA(variable1, variable2, variable3);cout << "variable1 [" << variable1 << "] variable2 [" << variable2 << "] variable3 [" << variable3 << "]" << endl;

cout << "end solution 1" << endl;

return ok;}

Page 4: First Look at Pointers

Function A is a function

int functionA(int var1, int var2, int var3){cout << endl << endl << "in function A" << endl;cout << "var1 [" << var1 << "] var2 [" << var2 << "] var3 [" << var3 << "]" << endl;var1 = 11;var2 = 22;var3 = 33;cout << "var1 [" << var1 << "] var2 [" << var2 << "] var3 [" << var3 << "]" << endl;cout << "end of function A" << endl << endl << endl;return ok;}

Page 5: First Look at Pointers

Function Parameter Call By Value

When we return to main from Function A, what has happened to our original variables?

What has happened to the variables in Function A?

Page 6: First Look at Pointers

Call by Value: passing a vectorint do_solution2(){// create a vector with some words, in an order you will recognizevector<string> callByValue;// populate that vector, in the order you will recognizecallByValue.push_back("the");...callByValue.push_back("dogs");

// write it out, just to prove itfor (int index = 0; index < (int) callByValue.size(); index++){cout << callByValue[index] << " " ;}cout << endl;

functionB(callByValue);

// write it out, just to prove itfor (int index = 0; index < (int) callByValue.size(); index++){cout << callByValue[index] << " " ;}cout << endl;return ok;}

Page 7: First Look at Pointers

Call By Value: Receiving a Vectorint functionB(vector<string> paramVector){cout << endl << endl << "in function B" << endl;int swaps, index, upperIndex, lowerIndex;int sizeVector = (int) paramVector.size();upperIndex = sizeVector - 1;lowerIndex = 0;for (swaps = 0; swaps <= (sizeVector/2) ; swaps++){string temp;temp = paramVector[lowerIndex];paramVector[lowerIndex] = paramVector[upperIndex];paramVector[upperIndex] = temp;lowerIndex++;upperIndex--;}// write it out, just to prove itfor (index = 0; index < (int) sizeVector; index++){cout << paramVector[index] << " " ;}cout << endl;return ok;}

Page 8: First Look at Pointers

Call By Value Features

Some benefits of call by value:

Keeps your variables safe. You can't corrupt variables in one function with mistakes in another.

How? Because the mechanism makes a copy of your variables.

Page 9: First Look at Pointers

Call By Value: Copies

What other implications can you think of?

Copying small things, (integers) is not expensive.

Copying big things, (such as a very long vector) is expensive.

What if I want my function to make a permanent change to a variable? What if that's the behavior I am trying to code?

Sometimes call by value is not the right technique for the job.

Page 10: First Look at Pointers

Call By Reference

If call by value makes a copy, then what do you think call by value does?

How do you think this happens?

Page 11: First Look at Pointers

Call by Value, and Streams

If you try to copy a stream, what happens?

compilation error.

What does it say? Is it cryptic to you?

Error 1 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' c:\program files\microsoft visual studio 8\vc\include\fstream 675

Page 12: First Look at Pointers

Call by Reference, Streams

change that code so that the streams are not called by value,(meaning, they are not copied)

In this way, you don't mistakenly try to copy private values(which the compiler disallows.)

Just send a reference of the stream.

int process_file(ifstream &myInStream);

int process_file(ifstream &myInStream){  bla , bla, bla ...