// --- Example x12: Functions. Arguments by value /* Demonstrates that a function has its own local variables. Arguments passed to it by value are NOT shared variables. Only the value of the variable is received at run time. ----------------------------------------------- */ #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 " << "\n UNCHANGED by the call to \"double_value().\" \n "; return 0; }