// --- Example: Functions. Arguments by reference /* Demonstrates that arguments passed by reference are SHARED variables. Any change to the variable's value in the function affects the variable's value in main as well. It's the SAME variable, even though the calling and called functions may (or may not) use different names for it. ----------------------------------------------- */ #include int double_value ( int & i ) { /* This function uses type conversion to add its arguments and return the sum. It illustrates the locality of the variables in its argument list. */ cout << "\n ---------------------------- "; cout << "\n Beginning function \"double_value( int& i )\""; cout << "\n ---------------------------- "; cout << "\n i = " << i ; cout << "\n Doubling...\n"; i += i; cout << "\n Now i = " << i ; cout << "\n ---------------------------- "; cout << "\n End function \"double_value()\""; cout << "\n ----------------------------\n "; return i; } int main(void) { int imain = 5; cout << "\n ----------------------------\n "; cout << " Beginning function \"main()\""; cout << "\n ---------------------------- "; cout << "\n imain = " << imain ; // cout << "\n ----------------------------\n " cout << " \n Calling function: double_value( imain )"; // cout << "\n ---------------------------- "; int jmain = double_value( imain ); cout << " ----------------------------\n " << " Back in function \"main()\""; cout << "\n ---------------------------- "; cout << "\n returned value (2*imain) = " << jmain; cout << "\n Now imain = " << imain ; cout << "\n Note: the value of imain is changed " << "\n by the call to \"double_value().\" \n "; return 0; }