c++ params

Reference parameters give the function access to the actual value being passed - not a copy of the value as with value parameters. Pass by reference allows a function to alter values directly in the space of the calling function.



.. value parameters

Normally, when a function is called, a COPY of the argument is made in the called function... Call ==== funcA (arg1, arg2); | | Function copy | | copy ======== | --------| V V void funcA ( int param1, int param2 ) { // If the function CHANGES any of the passed values: param1 = 0; param2 = 999; return; } It has NO effect on the original arguments in the calling function.
EXAMPLE: pass by value ======== // prototype: void, 1 parameter void hello( string ); int main() { // CALL: hello( "Susie" ); ^^^^^ //CALL: string who = "kris"; hello( who ); ^^^ } // definition: void, 1 parameter void hello(string name) { ^^^^ cout << "hello " << name << endl; }
EXAMPLE: pass by VALUE ======== // value-returning, 1 parameter int doubsq( int ); // prototype int main ( ) { ... num = 3; ... x = doubsq( num ); // call num COPIED to val ^^^ cout << x; // output will be 36 cout << num; // output will be 3 - no change in num } int doubsq( int val ) // definition { ^^^ val = val * 2; val = val * val; return val; }

.. reference parameters

Reference parameters are useful in three situations: 1. Returning more than one value 2. Changing the actual argument passed in 3. When passing the address would save memory space and time (arrays) In pass by reference, the address of the arg is passed to function. ^^^^^^^ This is denoted by the use of the "&" immediately after the type in BOTH the prototype and the function definition.
Call ==== funcA( arg1, arg2 ); | | Function address| | address ======== | --------| V V void funcA ( int& param1, int& param2 ) { // Here, 'param1' and 'param2' are reference parameters. // If the function CHANGES any of the passed values: param1 = 0; // changes arg1 param2 = 999; // changes arg2 return; } It DOES HAVE AN effect on the original arguments in the calling function.
EXAMPLE: pass by REFERENCE ======== // void 1 parameter void doubsq( int& ); // prototype int main( ) { ... num = 3; cout << num; // output will be 3 ... doubsq( num ); // call - address of num given to function ^^^ cout << num; // output will be 36 } void doubsq( int& val ) // definition { ^^^ val = val * 2; // changes num val = val * val; // changes num return; }
EXAMPLE: pass by REFERENCE ======== PROTOTYPE ========= void Init( int&, int&, int& ); CALL(s) ==== Init( winnings, total, grandtotal ); ... Init( x, y, z ); ... Init( num1, num2, sum ); DEFINITION ========== // This function initializes variables to zero //--------------------------------------------- void Init( int& x, int& y, int& z ) { // by using reference params, we can zero any 3 passed params x = 0; y = 0; z = 0; return; }

.. code: reads and prints - why reference?

//This program reads a course score and prints the associated course grade. #include <iostream> using namespace std; void getScore( int& score ); void printGrade( int score ); int main( void ) { int courseScore; // get score cout << "Based on the score, this " << "computes the course grade.\n"; getScore( courseScore ); // find and show associated grade printGrade( courseScore ); return 0; } // prompt & get score from user void getScore( int& score ) { cout << "Enter course score: "; cin >> score; cout << endl << "Course score is " << score << endl; return; } // RETURN AN int: prompt & get score from user int getScore( void ) { int score; cout << "Enter course score: "; cin >> score; cout << endl << "Course score is " << score << endl; return score; } // show course grade using 90/80/70/60 scale void printGrade( int cScore ) { cout << "Grade for the course is "; if ( cScore >= 90 ) cout << "A." << endl; else if ( cScore >= 80 ) cout << "B." << endl; else if( cScore >= 70 ) cout << "C." << endl; else if ( cScore >= 60 ) cout << "D." << endl; else cout << "F." << endl; return; }

.. default params

When a function is called - The number of actual and formal parameters must be the same - C++ relaxes this condition for functions with default parameters Specify value of a default parameter when the function name appears for first time, such as in the prototype If parameter not specified - the default value is used All default parameters must be the rightmost parameters of the function Default values can be constants, global variables, or function calls The caller can specify a value other than default for any default parameter // What does this do? #include <iostream> using namespace std; int volume ( int l = 1, int w = 1, int h = 1 ); void funcOne ( int& x, double y = 12.34, char z = 'B' ); int main( ) { int a = 23; double b = 48.78; cout << "Volume = " << volume( ) << endl; // 1 cout << "Volume = " << volume( 5, a ) << endl; // 115 cout << "Volume = " << volume( 34 ) << endl; // 34 cout << "Volume = " << volume( 6, 4, 5 ) << endl; // ? funcOne( a ); funcOne( a, b ); funcOne( a, 34.65, 'Q' ); return 0; } int volume ( int x = 1, int y = 1, int z = 1 ) { return x * y * z; } void funcOne ( int& x, double y = 12.34, char z = 'B' ) { // does stuff ... return; }
Other versions of volume( ) with different default settings... int volume( int x, int y, int z = 1 ) { return x * y * z; } Call can be: v = volume ( 2, 4, 5 ); // 40 v = volume ( 2, 4 ); // 8 v = volume ( 4 ); // NO v = volume ( ); // NO
int volume( int x, int y = 2, int z = 1 ) { return x * y * z; } Call can be: v = volume ( 2, 4, 5 ); // 40 v = volume ( 2, 4 ); // 8 v = volume ( 4 ); // 8 v = volume ( ); // NO

.. function overloading

Overloading refers to the creation of several functions with the same name If several functions have the same name, each function must have: - A different number of parameters - If the number of parameters is the same, then the data type of the parameters must differ in at least one position //Sample code for function overloading void AddDisplay( int x, int y ) { cout << "Integer result: " << ( x+y ); } void AddDisplay( double x, double y ) // param types different { ^^^^^^^^^^^ cout << "Double result: " << ( x+y ); } void AddDisplay( float x, float y )// param types different { ^^^^^^^^^^^ cout << "Float result: " << ( x+y ); } void AddDisplay( int x )// param NUMBER different { ^^^^^^ cout << "Integer result: " << ( x ); }

.. code examples