Thread: function as parmeter

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    24

    function as parmeter

    Code:
    double f(double a){return a;} 
    int main(void) {
      double d1,d2; 
    derivatives(f(1.0),1, 0.0001,&d1,&d2); 
      
        return (0); 
    } /* end main */
    Code:
    int derivatives(double f(double a), double x, double eps, double * d1, double * d2){
        
        (*d1)=(f(x+eps)-f(x-eps))/(2*eps);
        printf("%lf",*d1);
        (*d2)=(f(x+eps)-2*f(x)+f(x-eps))/(eps*eps);
    
        
    }
    error says incomtabile for parameter 1
    any help please ?

  2. #2
    Registered User
    Join Date
    Apr 2018
    Posts
    5
    In the standard C language, you can not insert functions into parameters, use macros and defines.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Washington View Post
    In the standard C language, you can not insert functions into parameters, use macros and defines.
    C supports passing a function pointer to another function, which seems to be the intent here.

    Change "double f(double a)" to "double (*f)(double a)", and then change "f(1.0)" in the call to derivatives to just "f".

    Your code should look more like this:

    Code:
    int derivatives(double (*f)(double a), double x, double eps, double * d1, double * d2);
    
    derivatives(f,1, 0.0001,&d1,&d2);

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    You can use a pointer to a function and also pass the parameter(s) which go with that function.
    It does not look nice/readable but it can be done.
    For example:

    Code:
    #include <stdio.h>
    
    void doWhat(int (*fPtr) (), int param1)
    {
        printf("%d\n", (*fPtr)(param1));
    }
    
    int intFunc(int a)
    {
        return a * 2;
    }
    
    int main(void)
    {
        int (*pt_iFunc) ();
    
        pt_iFunc = intFunc;
    
        doWhat(pt_iFunc, 5);
    
        return (0);
    }

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    sorry christop, while I was typing you already sent an answer

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You don't actually *need* the weird declaration for the function pointer. The following is 100% valid C:

    Code:
    #include <stdio.h>
    
    int foo(int x)
    {
        return x*x;
    }
    
    int bar(int func(int), int x)
    {
        return func(x);
    }
    
    int main(void)
    {
        printf("%d\n", bar(foo, 2));
        return 0;
    }
    Code:
    > gcc --std=c89 -Wall -pedantic blah.c 
    > 
    > ./a.out
    4
    >
    Without looking too much into it I think the OP's problem was having
    Code:
    int derivatives(double f(double a), double x, double eps, double * d1, double * d2)
    Rather than
    Code:
    int derivatives(double f(double), double x, double eps, double * d1, double * d2)
    (not that in the first instance it names the parameter and in the second one that should work it only has the type

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Hodor View Post
    You don't actually *need* the weird declaration for the function pointer.
    Wow, you're right! I honestly didn't know that, though I personally prefer using the "weird" declaration because it's the same syntax you have to use when you declare a function pointer in any other context.

    Quote Originally Posted by Hodor View Post
    Without looking too much into it I think the OP's problem was having
    Code:
    int derivatives(double f(double a), double x, double eps, double * d1, double * d2)
    Rather than
    Code:
    int derivatives(double f(double), double x, double eps, double * d1, double * d2)
    (not that in the first instance it names the parameter and in the second one that should work it only has the type
    Giving the function pointer's parameter a name isn't an error, as I just tested it with "gcc -Wall -std=c89 -pedantic". The only problem in the OP's code was in the invocation of the "derivatives" function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  5. how to pass a function as a parmeter to a function?
    By billy_other in forum C Programming
    Replies: 2
    Last Post: 04-25-2005, 12:17 AM

Tags for this Thread