/********************************************************/ // R. A. Hillyard // funIntro1b.cpp // intro to features and requirements // of user defined functions // pass-by-value parameters // use the "&" address of operator to illustrate scope // of pass-by-value parameters /********************************************************/ #include<iostream> using namespace std; const double GlobalOne = 9.7531; double min(double x, double y); //function prototype - note parameter names int main() { double x = 3.66, y = 3.60, c = 0.0; cout << "In main:\n at address: " << &x << " x = " << x << endl; cout << " at address: " << &y << " y = " << y << endl; cout << " at address: " << &c << " c = " << c << endl; cout << " at address: " << &GlobalOne << " GlobalOne = " << GlobalOne << endl; //function call - return value will be assigned to c c = 100.0 + min(x, y); //function call with actual parameters cout << "\nc = " << c << "\n\n"; cout << "In main:\n at address: " << &x << " x = " << x << endl; cout << " at address: " << &y << " y = " << y << endl; cout << " at address: " << &c << " c = " << c << endl; cout << " at address: " << &GlobalOne << " GlobalOne = " << GlobalOne << endl; return 0; } /********************************************************/ //min - returns the smallest of two floating point numbers /********************************************************/ double min(double x, double y) { cout << "In function:\n at address: " << &x << " x = " << x << endl; cout << " at address: " << &y << " y = " << y << endl; cout << " at address: " << &GlobalOne << " GlobalOne = " << GlobalOne << endl; //The variable c is not visible here //compiler will issue an "undefined identifier" error //cout << " at address: " << &c << " c = " << c << endl; y = GlobalOne; if(x < y) return x; return y; }//end min /********************************************************/ //program output /********************************************************/ In main: at address: 0x0012ff84 x = 3.66 at address: 0x0012ff8c y = 3.6 at address: 0x0012ff94 c = 0 at address: 0x0041b020 GlobalOne = 9.7531 In function: at address: 0x0012ff74 x = 3.66 at address: 0x0012ff7c y = 3.6 at address: 0x0041b020 GlobalOne = 9.7531 c = 103.66 In main: at address: 0x0012ff84 x = 3.66 at address: 0x0012ff8c y = 3.6 at address: 0x0012ff94 c = 103.66 at address: 0x0041b020 GlobalOne = 9.7531 /********************************************************/