Thread: Define new function as product of two functions

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Define new function as product of two functions

    Hello everyone, I am trying to write a function in C which takes in two (arbitrary) function pointers and returns a pointer to a function which is the product of the two initial (arbitrary) functions. For example, if I have two arbitrary functions f(x) and g(x), my new function h(x) should equal f(x)*g(x). I do not want to evaluate the functions at any point, only after I have defined the new function h(x). Can anyone help me?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int DynaFunc(void *func1, void *func2, int x)
      { return func1(x) * func2(x); }
    
    
    //call as
    
    z = Dynafunc(&f,&g, 31);

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is not possible to create new functions in C at run time.

    The closest you can come is to store the supplied f() and g() in some static variables (of type pointer to function) that are accessible by a function named h(). Implement h() so it uses those two static function pointers, multiplies their results together, and returns that. There is a trade-off that, if you call your function with different another_f() and another_g(), that a previously returned h(x) will quietly start returning the value of another_f(x)*another_g(x) rather than the original f(x)*g(x). That is a consequence of using static variables.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    CommonTater's code is not going to work. You can't pass a void* and call it as if it was a function (at least not without casting the pointer).

    You can't create a function that takes a function as an input and returns a new one because C hasn't support for "anonymous functions". You can simulate it by using a struct that wraps the input functions or by having global state, though.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ronix View Post
    CommonTater's code is not going to work. You can't pass a void* and call it as if it was a function (at least not without casting the pointer).

    You can't create a function that takes a function as an input and returns a new one because C hasn't support for "anonymous functions". You can simulate it by using a struct that wraps the input functions or by having global state, though.
    You're right... my bad. I was just wanting to show an example, and should have tested it first.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Ronix View Post
    You can't create a function that takes a function as an input and returns a new one because C hasn't support for "anonymous functions". You can simulate it by using a struct that wraps the input functions or by having global state, though.
    In C++, I would have suggested using a (possibly templated) functor. That is more or less the same as a wrapper struct, except that a functor can support the same calling syntax, but a wrapper struct in C cannot.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. define functions in another file
    By suryak in forum C Programming
    Replies: 14
    Last Post: 08-09-2011, 12:59 AM
  2. #define function
    By begin in forum C Programming
    Replies: 14
    Last Post: 06-18-2010, 03:35 PM
  3. define two functions
    By noob programmer in forum C Programming
    Replies: 10
    Last Post: 11-04-2008, 01:41 PM
  4. Replies: 7
    Last Post: 04-17-2008, 12:54 PM
  5. Help! Sample prog. on user-define string functions
    By orbandjay in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2002, 02:48 AM