
-----------------------------------
Tubs
Wed Nov 09, 2005 4:53 pm

Function as a parameter
-----------------------------------
Though I *believe* I have declared all the parameters the correct type and everything else, the program still says that the first argument of eval(); is incorrect (I'm just learning how to input a function as a parameter so it is probably just a syntax problem). Any help or tips would be appreciated as always  :wink: Dev C++ 


#include 

void eval( double f( double farg ), double start, double step, int count);
double g (double farg);
double h (double farg);

int main(int argc, char *argv[])
{

double step, i, function, x;
int count, start;

printf ("What is the starting value of x> ");
scanf ("%d", &start);
printf ("What is the increment that x increases by> ");
scanf ("%lf", &step);
printf ("How many values shall be displayed> ");
scanf ("%d", &count);

printf ("g(x) for x = %d, %.2f, %d\n", start, step, count);
printf ("\n");
printf ("x    f(x)\n");
printf ("--   ---\n");

eval(g(x), start, step, count);

system ("PAUSE");

  return 0;
}

double g (double farg)

{
return (5 * pow(farg, 3.0) - 2 * pow(farg, 2.0) + 3);
}

double h (double farg)

{
return (pow(farg, 4.0) - 3 * pow(farg, 2.0) - 8);
}


-----------------------------------
[Gandalf]
Wed Nov 09, 2005 5:08 pm


-----------------------------------
Well, you don't pass a function to another function, you pass the result of the function.  So, if you have something returning a double, and you assign a variable, f, that result, then you would just pass f to the function.

Hmm... I was going to give an example, but it seems you don't even have a function eval() in that code :?

-----------------------------------
rizzix
Wed Nov 09, 2005 5:15 pm


-----------------------------------
nay, you can pass functions as parameters.. but he's doing it all wrong..

-----------------------------------
[Gandalf]
Wed Nov 09, 2005 5:28 pm


-----------------------------------
Really?  Interesting...  :)
Well, I'm pretty sure he just wants to pass the result to the function.  Besides, where is this eval()?

-----------------------------------
Tubs
Wed Nov 09, 2005 5:56 pm


-----------------------------------
sorry.


void eval( double f( double farg ), double start, double step, int count)

{

double i;

for (i = start; i < count; i + step);

    {
    printf ("%.1f   %.1f\n", i, (g(i));
    }

}


-----------------------------------
[Gandalf]
Wed Nov 09, 2005 6:55 pm


-----------------------------------
The point remains...

Well, you don't pass a function to another function, you pass the result of the function. So, if you have something returning a double, and you assign a variable, f, that result, then you would just pass f to the function. 

-----------------------------------
Tubs
Wed Nov 09, 2005 7:00 pm


-----------------------------------
Yes, that is what I would do when approaching this problem. BUT!

Write a function eval() which takes another function as a parameter and evaluates it over a range of values. The prototype for eval() is:

void eval( double f( double farg ), double start, double step, int count )



Question says otherwise.  :(

-----------------------------------
Tubs
Thu Nov 10, 2005 12:12 am


-----------------------------------
no idea? this is boggling me.

-----------------------------------
wtd
Thu Nov 10, 2005 12:37 am


-----------------------------------
When you declare a pointer to a function, it looks like:

double f( double farg )

This says you have an argument called "f" which takes a "double" argument, and returns a "double".  

So when you pass the argument... you just pass the name of the function.

-----------------------------------
Tubs
Thu Nov 10, 2005 1:20 pm


-----------------------------------
Have i ever told you how i love you wtd
