How do I send a function to a function

This is a discussion on How do I send a function to a function within the C++ Programming forums, part of the General Programming Boards category; I apologize for my ignorance on how to talk about this stuff but I haven't programmed for a few years ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    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.

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    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;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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;
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    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 .

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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
    Last edited by Dave Evans; 04-13-2005 at 09:47 AM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Maybe something like this would meet your needs:
    In C++, you have to declare something before you can use it.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    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

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Could this "Simpson's rule" actually be written in C++

    No it can't because you would be:

    seeing a revelation of Christ Jesus?
    which is impossible.


  11. #11
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    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 ?

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    }
    Last edited by 7stud; 04-14-2005 at 02:52 PM.

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending n no of char data uning send() function,
    By thebrighter in forum Windows Programming
    Replies: 1
    Last Post: 08-22-2007, 12:26 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21