Function as a parameter
Author |
Message |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Wed Nov 09, 2005 4:53 pm Post subject: 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 Dev C++
code: |
#include <stdio.h>
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);
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Wed Nov 09, 2005 5:08 pm Post subject: (No subject) |
|
|
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 ![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif) |
|
|
|
|
![](images/spacer.gif) |
rizzix
|
Posted: Wed Nov 09, 2005 5:15 pm Post subject: (No subject) |
|
|
nay, you can pass functions as parameters.. but he's doing it all wrong.. |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Wed Nov 09, 2005 5:28 pm Post subject: (No subject) |
|
|
Really? Interesting...
Well, I'm pretty sure he just wants to pass the result to the function. Besides, where is this eval()? |
|
|
|
|
![](images/spacer.gif) |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Wed Nov 09, 2005 5:56 pm Post subject: (No subject) |
|
|
sorry.
code: |
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));
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Wed Nov 09, 2005 6:55 pm Post subject: (No subject) |
|
|
The point remains...
Quote: 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. |
|
|
|
|
![](images/spacer.gif) |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Wed Nov 09, 2005 7:00 pm Post subject: (No subject) |
|
|
Yes, that is what I would do when approaching this problem. BUT!
Quote: 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. ![Sad Sad](http://compsci.ca/v3/images/smiles/icon_sad.gif) |
|
|
|
|
![](images/spacer.gif) |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Thu Nov 10, 2005 12:12 am Post subject: (No subject) |
|
|
no idea? this is boggling me. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Thu Nov 10, 2005 12:37 am Post subject: (No subject) |
|
|
When you declare a pointer to a function, it looks like:
c: | 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. |
|
|
|
|
![](images/spacer.gif) |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Thu Nov 10, 2005 1:20 pm Post subject: (No subject) |
|
|
Have i ever told you how i love you wtd |
|
|
|
|
![](images/spacer.gif) |
|
|