80 likes | 152 Views
cop3530sp12. 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
E N D
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<int> & arr, int n, bool & errorFlag) (// source: Mark Allen Weiss' DSA book) Also see the three swap functions described in class.
Return Passing • Return by Value • Return by constant reference • return by reference (occasionally) • using pointers (see the 2d dynamic array allocation example).
// 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 maxValueis declared inside the function and so returning by reference or constant reference is not correct. Return by value would have been correct. #include <string> #include <iostream> #include <vector> using namespace std; const string & findMax( const vector<string> & arr ) { intmaxIndex = 0; for( inti = 1; i < arr.size( ); i++ ) if( arr[ maxIndex ] < arr[ i ] ) maxIndex = i; return arr[ maxIndex ]; } const string & findMaxWrong( const vector<string> & arr ) { string maxValue = arr[ 0 ]; for( inti = 1; i < arr.size( ); i++ ) if( maxValue < arr[ i ] ) maxValue = arr[ i ]; return maxValue; } // continued on next slide
// ... continued from previous slide int main( ) { vector<string> arr; arr.push_back( "hello" ); arr.push_back( "there" ); arr.push_back( "world" ); cout << findMax( arr ) << endl; cout << findMaxWrong( arr ) << endl; return 0; }
// 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.
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 • // pass by reference -- works
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.