#include // Take in a pointer-to-a-function, two arguments for the function, // and apply the function. Return the result. double applyFunction (double (*func)(double, double), double x, double y) { double z; // For result. z = (*func) (x, y); // Apply the function. Note the parentheses around "*func". return z; } // How to declare a function prototype when an argument is a pointer-to-function: char * funcName ( double (*func)(double, double) ); //--------------------------------------------------------------------- // Test code. // A test function. Take in a pointer-to-a-function, apply it, // print it's name and result of applying the function. void test ( double (*func)(double, double) ) { double x, y, z; char *name; x = 1.0; y = 2.0; // Test values. z = applyFunction (func, x, y); // Send the function itself to "applyFunction". name = funcName (func); // Likewise to "funcName". printf ("x = %lf y = %lf %s(x,y) = %lf\n", x, y, name, z); } // Some function prototypes that will be used in testing. double max (double, double); double min (double, double); double absDiff (double, double); int main () { test (max); test (min); test (absDiff); } //--------------------------------------------------------------------- // The function implementations: double max (double x, double y) { if (x > y) return x; else return y; } double min (double x, double y) { if (x < y) return x; else return y; } double absDiff (double x, double y) { if (x > y) return (x - y); else return (y - x); } // Take in a pointer-to-a-function and print its name. char * funcName ( double (*func)(double, double) ) { if (func == max) { return "maximum"; } else if (func == min) { return "minimum"; } else if (func == absDiff) { return "absoluteDifference"; } }