// Ex721.cc // Sec. 7.2 of lecture // Demonstration of function parameters #include // declare function sgn( ) double sgn(double x); // by value //double sgn(double& x); // by reference //double sgn(const double& x); // by reference, const. data // declare function sgn( * ) double sgn(double* x); // by address //double sgn(const double* x); // by address, const. data //double sgn(const double* const x); // by address, const. pointer //------------------------------------------ main() { double a,b, *pa=&a; cout << endl << " Input Argument : "; cin >> a; b = sgn(a); // by value or by reference cout << " 1: sgn(" << a << ") = " << b << endl; b = sgn(&a); // by address cout << " 2: sgn(" << a << ") = " << b << endl; if (pa==&a) { cout << endl << "Adress of x remains the same." << endl; } } //------------------------------------------ // declare functions double sgn(double x) // value //double sgn(double& x) // reference //double sgn(const double& x) // const. reference { double y; if ( x > 0.0 ) { y = 1.0 ; } else { if ( x == 0.0 ) { y = 0.0 ; } else { y = -1.0 ; } } x++; // Demo with different parameter types return y; } //------------------------------------------ double sgn(double* x) // by address //double sgn(const double* x) // by address, const. data //double sgn(const double* const x) // by address, const. pointer { double y; if ( *x > 0.0 ) { y = 1.0 ; } else { if ( *x == 0.0 ) { y = 0.0 ; } else { y = -1.0 ; } } (*x)++; // Demo with different parameter types x++; // Demo with different parameter types return y; }