Originally Posted by

**Signifier**
I apologize for my ignorance on how to talk about this stuff but I haven't programmed for a few years and I've lost the lingo.

Anyway, I'm trying to write a function to estimate definite integrals and I don't know how to send a function to a function. Um... what I mean is, I want to write a function which will accept as one of its arguments any function of type double f (double x).

The best I can remember how to do this involves pointers and the address-of operator, but I've completely forgotten where to put the * or the &, or if I have to use the * operator multiple times.

If no one has any idea what I'm talking about, I'll try to explain myself better. Thanks in advance for any help.

The first response (by Mortissus) showed the way: the argument is a pointer to a function.

Once you figure out the syntax of the declaration, it's easy: just use the name of the function in the argument list, since the name of the function by itself is treated as "pointer to function".

Maybe something like this would meet your needs:

Code:

#include <iostream>
using std::cout;
using std::endl;
int main()
{
double simp(double (*f)(double), double start, double end, int steps);
double g(double x);
double value;
double first = 0.0;
double last = 1.0;
int NumberOfIntervals = 10;
value = simp(g, first, last, NumberOfIntervals);
cout << "value = " << value << endl;
return 0;
}
// This is the user-defined function to be integrated
//
double g(double x)
{
return x*x;
}
// Use simpson's rule to approximate the integral from
// start to end, using steps
//
double simp (double (*f)(double), double start, double end, int steps)
{
// Simpson's rule here
}

The first argument of simp() is a pointer to a function. That function is of type "double" and has a single argument, which is a double.

Regards,

Dave