Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.

Similar presentations


Presentation on theme: "Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate."— Presentation transcript:

1 cop3530sp12

2 Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate for large objects that should not be altered by the function call by reference - appropriate for all objects that should be altered by the function using pointers -- see the swap method explained in class We discussed this example in class. We don’t want the contents of of the vector arr to change so we pass it by constant reference. The value of errorFlag is passed by reference because we want that the change in its value should be refelected in the calling function. double avg (const vector & arr, int n, bool & errorFlag) (// source: Mark Allen Weiss' DSA book) Also see the three swap functions described in class.

3 Return Passing Return by Value Return by constant reference return by reference (occasionally) using pointers (see the 2d dynamic array allocation example).

4 // note that in the correct version we are returning a string by constant reference which was not declared inside the function, so when the function returns the tsring doesn’t go out of scope. In the incorrect version, terrible things may happen as the string maxValue is declared inside the function and so returning by reference or constant reference is not correct. Return by value would have been correct. #include using namespace std; const string & findMax( const vector & arr ) { int maxIndex = 0; for( int i = 1; i < arr.size( ); i++ ) if( arr[ maxIndex ] < arr[ i ] ) maxIndex = i; return arr[ maxIndex ]; } const string & findMaxWrong( const vector & arr ) { string maxValue = arr[ 0 ]; for( int i = 1; i < arr.size( ); i++ ) if( maxValue < arr[ i ] ) maxValue = arr[ i ]; return maxValue; } // continued on next slide

5 //... continued from previous slide int main( ) { vector arr; arr.push_back( "hello" ); arr.push_back( "there" ); arr.push_back( "world" ); cout << findMax( arr ) << endl; cout << findMaxWrong( arr ) << endl; return 0; }

6 // using pass by value – doesn’t work void swap (int a, int b) { int temp = a ; a = b ; b = temp ; } int main () { int p = 6 ; int q = 7 ; swap(p, q) ; cout << p ; //outputs 6 cout << q ; //outputs 7 ; //This is called call by value and no change happens. // this doesn't work. The change that happens inside the function is local to tha function and is not reflected in the calling function main.

7 // pass by reference -- works void swap (int & a, int & b) { int temp = a ; a = b ; b = temp ; } int main() { int p = 6 ; int q = 7 ; swap (p, q) ; cout << p ; // prints 7 cout << q ; // prints 6 //call by reference // this works

8 void swap (int *a, int *b) { temp = *a ; *a = *b ; *b = temp ; } int main() { int p = 6 ; int q = 7 ; swap(&p, &q) ; cout << p ; //prints 7 cout << q ; // prints 6 // Using pointers. This is actually a Cstyle swap code. IN C always passby value happens and so the address of p and q gets copied into a and b. So, even though it is pass by value we are still able to accomplish the swapping using pointers.


Download ppt "Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate."

Similar presentations


Ads by Google