Upcoming SlideShare
×

# First Look at Pointers

496 views

Published on

Call by Reference, Call by Value

Published in: Lifestyle, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
496
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
16
0
Likes
0
Embeds 0
No embeds

No notes for slide

### First Look at Pointers

1. 1. First Look at Pointers
2. 2. Function Parameter Call by Value <ul><li>When a function is invoked and the function takes parameters, how do those parameters relate to the original values in the calling function? </li></ul><ul><li>main (itself a function) has variables 1, 2 and 3. </li></ul><ul><li>Main calls Function A with parameters 1, 2, and 3, for which we fill in the values of variables 1, 2, and 3. </li></ul><ul><li>Function A goes about its business, including changing the parameters. </li></ul><ul><li>Then we return to main. </li></ul>
3. 3. Main is a function <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>string msg = &quot;do solution1&quot;; </li></ul><ul><li>cout << msg << endl; </li></ul><ul><li>int variable1, variable2, variable3; </li></ul><ul><li>variable1 = 1; </li></ul><ul><li>variable2 = 2; </li></ul><ul><li>variable3 = 3; </li></ul><ul><li>cout << &quot;variable1 [&quot; << variable1 << &quot;] variable2 [&quot; << variable2 << &quot;] variable3 [&quot; << variable3 << &quot;]&quot; << endl; </li></ul><ul><li>functionA(variable1, variable2, variable3); </li></ul><ul><li>cout << &quot;variable1 [&quot; << variable1 << &quot;] variable2 [&quot; << variable2 << &quot;] variable3 [&quot; << variable3 << &quot;]&quot; << endl; </li></ul><ul><li>cout << &quot;end solution 1&quot; << endl; </li></ul><ul><li>return ok; </li></ul><ul><li>} </li></ul>
4. 4. Function A is a function <ul><li>int functionA(int var1, int var2, int var3) </li></ul><ul><li>{ </li></ul><ul><li>cout << endl << endl << &quot;in function A&quot; << endl; </li></ul><ul><li>cout << &quot;var1 [&quot; << var1 << &quot;] var2 [&quot; << var2 << &quot;] var3 [&quot; << var3 << &quot;]&quot; << endl; </li></ul><ul><li>var1 = 11; </li></ul><ul><li>var2 = 22; </li></ul><ul><li>var3 = 33; </li></ul><ul><li>cout << &quot;var1 [&quot; << var1 << &quot;] var2 [&quot; << var2 << &quot;] var3 [&quot; << var3 << &quot;]&quot; << endl; </li></ul><ul><li>cout << &quot;end of function A&quot; << endl << endl << endl; </li></ul><ul><li>return ok; </li></ul><ul><li>} </li></ul>
5. 5. Function Parameter Call By Value <ul><li>When we return to main from Function A, what has happened to our original variables? </li></ul><ul><li>What has happened to the variables in Function A? </li></ul>
6. 6. Call by Value: passing a vector <ul><li>int do_solution2() </li></ul><ul><li>{ </li></ul><ul><li>// create a vector with some words, in an order you will recognize </li></ul><ul><li>vector<string> callByValue; </li></ul><ul><li>// populate that vector, in the order you will recognize </li></ul><ul><li>callByValue.push_back(&quot;the&quot;); </li></ul><ul><li>... </li></ul><ul><li>callByValue.push_back(&quot;dogs&quot;); </li></ul><ul><li>// write it out, just to prove it </li></ul><ul><li>for (int index = 0; index < (int) callByValue.size(); index++) </li></ul><ul><li>{ </li></ul><ul><li>cout << callByValue[index] << &quot; &quot; ; </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>functionB(callByValue); </li></ul><ul><li>// write it out, just to prove it </li></ul><ul><li>for (int index = 0; index < (int) callByValue.size(); index++) </li></ul><ul><li>{ </li></ul><ul><li>cout << callByValue[index] << &quot; &quot; ; </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>return ok; </li></ul><ul><li>} </li></ul>
7. 7. Call By Value: Receiving a Vector <ul><li>int functionB(vector<string> paramVector) </li></ul><ul><li>{ </li></ul><ul><li>cout << endl << endl << &quot;in function B&quot; << endl; </li></ul><ul><li>int swaps, index, upperIndex, lowerIndex; </li></ul><ul><li>int sizeVector = (int) paramVector.size(); </li></ul><ul><li>upperIndex = sizeVector - 1; </li></ul><ul><li>lowerIndex = 0; </li></ul><ul><li>for (swaps = 0; swaps <= (sizeVector/2) ; swaps++) </li></ul><ul><li>{ </li></ul><ul><li>string temp; </li></ul><ul><li>temp = paramVector[lowerIndex]; </li></ul><ul><li>paramVector[lowerIndex] = paramVector[upperIndex]; </li></ul><ul><li>paramVector[upperIndex] = temp; </li></ul><ul><li>lowerIndex++; </li></ul><ul><li>upperIndex--; </li></ul><ul><li>} </li></ul><ul><li>// write it out, just to prove it </li></ul><ul><li>for (index = 0; index < (int) sizeVector; index++) </li></ul><ul><li>{ </li></ul><ul><li>cout << paramVector[index] << &quot; &quot; ; </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>return ok; </li></ul><ul><li>} </li></ul>
8. 8. Call By Value Features <ul><li>Some benefits of call by value: </li></ul><ul><li>Keeps your variables safe. You can't corrupt variables in one function with mistakes in another. </li></ul><ul><li>How? Because the mechanism makes a copy of your variables. </li></ul>
9. 9. Call By Value: Copies <ul><li>What other implications can you think of? </li></ul><ul><li>Copying small things, (integers) is not expensive. </li></ul><ul><li>Copying big things, (such as a very long vector) is expensive. </li></ul><ul><li>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? </li></ul><ul><li>Sometimes call by value is not the right technique for the job. </li></ul>
10. 10. Call By Reference <ul><li>If call by value makes a copy, then what do you think call by value does? </li></ul><ul><li>How do you think this happens? </li></ul>
11. 11. Call by Value, and Streams <ul><li>If you try to copy a stream, what happens? </li></ul><ul><li>compilation error. </li></ul><ul><li>What does it say? Is it cryptic to you? </li></ul><ul><li>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 filesmicrosoft visual studio 8vcincludefstream 675 </li></ul>
12. 12. Call by Reference, Streams <ul><li>change that code so that the streams are not called by value, </li></ul><ul><li>(meaning, they are not copied) </li></ul><ul><li>In this way, you don't mistakenly try to copy private values </li></ul><ul><li>(which the compiler disallows.) </li></ul><ul><li>Just send a reference of the stream. </li></ul><ul><li>int process_file(ifstream &myInStream); </li></ul><ul><li>int process_file(ifstream &myInStream) </li></ul><ul><li>{  </li></ul><ul><li>  bla , bla, bla ... </li></ul>