# How do I send a function to a function

• 04-12-2005
Signifier
How do I send a function to a function
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.
• 04-12-2005
Mortissus
As a general rule:

return_type (*f_name)(parameter)

Code:

```#include <stdio.h> int doStuff(int x){         return x+5; } int doSomethingWithStuff(int (*f)(int), int value){         return f(value); } int main(){         printf("Stuff is %d\n", doSomethingWithStuff(doStuff, 10));         getchar();         return 0; }```
• 04-12-2005
misplaced
try playing with this...i'm not sure the syntax is quite right, but i don't feel like testing it either
Code:

```int passed(int a, int b) {       return a+b; } int accepter(int x, int (*fname)(int, int)) {       return x + fname(4, 5);     } int main() {       cout << accepter(2, passed) << endl; }```
• 04-13-2005
arian
If I undrestand you right , it can also be as below :
Code:

```double fun1();      // it will be the parameter void fun2(double); int main(){   .   .   . fun2(fun1);      // when fun2 is called , it calls fun1 and then fun1                       //  will return a double value as fun2 parameter   .   .   .} double fun1(){   double a;   .   .   . return (a);}```
hope it helps .
• 04-13-2005
7stud
Quote:

Originally Posted by arian
If I undrestand you right , it can also be as below :
Code:

```double fun1();      // it will be the parameter void fun2(double); int main(){   .   .   . fun2(fun1);      // when fun2 is called , it calls fun1 and then fun1                       //  will return a double value as fun2 parameter   .   .   .} double fun1(){   double a;   .   .   . return (a);}```
hope it helps .

I hate to be the bearer of bad news, but your program won't even compile: fun1's type is not double, and therefore it cannot be an argument to fun2.

The poster want's to pass function1 to function2, and then presumably call function1 with some arguments inside function2(or assign it to something). Read the first two responses to see the syntax for passing a pointer to a function as an argument to another function.
• 04-13-2005
Zach L.
• 04-13-2005
Dave Evans
Quote:

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
• 04-13-2005
7stud
Quote:

Maybe something like this would meet your needs:
In C++, you have to declare something before you can use it.
• 04-13-2005
Signifier
Thanks everyone! I was forgetting the parentheses.

Oh, and Dave Evans:

You seem to know something about calculus. Could this "Simpson's rule" actually be written in C++ code as an estimation of the definite integral of f(x) from a to b? Without losing consciousness, and seeing a revelation of Christ Jesus?

Thanks
• 04-14-2005
treenef
Quote:

Could this "Simpson's rule" actually be written in C++

No it can't because you would be:

Quote:

seeing a revelation of Christ Jesus?
which is impossible.

;)
• 04-14-2005
arian
tnx for correcting me , but
Quote:

Originally Posted by 7stud
your program won't even compile: fun1's type is not double

can you plz tell me why ?
Code:

`double fun1();`
isn't fun1()'s type double ?
• 04-14-2005
7stud
Code:

```void fun2(double); fun2(fun1);```
fun2's parameter is a double, which is something like 3.2 or 1.6. In your code, you passed fun1 as an argument to fun2, but here is your declaration of fun1:
Code:

`double fun1();`
That is not a number like 3.2 or 1.6--it is a function. If instead you had defined fun1 like this:

double fun1 = 3.2;

then you could pass it to fun2. However, a function name, like with an array name, is the address of the function. Therefore, another function that accepts fun1 as an argument must list a pointer as its parameter. The type of the pointer parameter must be indicated using the syntax shown in the first two responses, which is:

double (*)(void)

So, you could declare fun2 like this:

void fun2( double (*)(void) );

and define it like this:
Code:

```void fun2( double (*pfun)(void) ) {       double result = pfun();       cout<<result<<endl; }```
• 04-14-2005
Signifier
Well, I'm estimating these things within six significant figures by using Riemann sums and really small delta-xs, so I suppose I don't need Simpson's rule anyway. No revelations.

Thanks again everyone!