/* Sample c++ code to illustrate passing of functions as parameter */ /* Otherwise, code is nonsense. */ /* Illustrates technique needed for Simpson's Rule assignment, */ /* CS 350 */ /* v. 980921 */ #include typedef float ( *pftn )( float ); float One ( float x ) { return( x ); } float Two( float x ) { return( 2*x ); } float Three( float x ) { return( 3*x ); } float PassFunctionParm( pftn ftn, float y ) { float value; value = ftn( y ); return( value ); } void main( void ) { float x; /* Call functions directly to see their effect */ x = 1; x = One( x ); cout << "One x: " << x << endl; x = Two( x ); cout << "Two x: " << x << endl; x = Three( x ); cout << "Three x: " << x << endl; /* Now again, but pass function name as parameter */ x = 1; x = PassFunctionParm( One, x ); cout << "Parm One x: " << x << endl; x = PassFunctionParm( Two, x ); cout << "Parm Two x: "<< x << endl; x = PassFunctionParm( Three, x ); cout << "Parm Three x: " << x << endl; }