954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pointer to function as an argument

Attached is a tiny program in which the function F has as an argument the function pointer double (*f) (double). F returns the sum f(1)+f(2)+f(3)+f(4).
(Naturally, I have a much more interesting application in mind, but this simple example makes the point.) What I really want to do is replace f by a function of two variables, say g(x,y). Then I'd compute g(1,y) + g(2,y)+g(3,y)+g(4,y), so that F() would be a function of y. Is there a way to do this? Default values almost do the trick, but not quite.

Another totally unrelated question is this: can I download a text file right into this window? Then you wwouldn't have to open an attachment to see the program.

Attachments test.cpp (0.31KB)
murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 21
Solved Threads: 1
 

Well, I have to tell you I suck at maths, so I dont get what you wanna do with the g(x,y) (mmm, I think I saw that in calculus 2 or 3 at collegue...)
Anyway, is it something like this what you wanna accomplish?

#include <iostream>
using namespace std;

double F(double (*f)(), int N,double);
double f(double,double);

double F(double (*f)(double,double), int N,double y)
{   double sum = 0;
    for(int i = 1; i <= N; i++)
        sum += (*f)(1.0*i,y);
    return sum;
}

double f(double x,double y)
{   return x*y;
}


int main(int argc,char* argv)
{   cout << F(f,4,6);

return 0;
}


And I answered your offtopic question... just put the sources inside code tags
(code)
Put your sources here
(/code)

Replace the () with []
That way, people wont have to download your attachment...

Cheers

darklordsatan
Light Poster
26 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

Thanks a lot! Your program works fine and it tells me just what I need to know. I thought that I'd have to say in the declaration of F that *f is a function of two variables and I didn't know how to do that, but apparently you don't have to. I guess it suffices to say that *f is a function which returns a double.

As to the tangential question about entering the code without using an attachment, I meant without typing the code in. I know how to type the code using tags, but I'd rather download the actual source file that ran so that I don't intoduce any typos. Of course, with a code as short as mine it wouldn't be much trouble to type it in and proofread very carefully, but with a bigger code that migh be a problem. I tried dragging it (i.e., dragging the filr icon from my computer) but that didnt work. Is there a way to paste it?

Once again, thanks.
PS. Dont knock your mathematical ability.

murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 21
Solved Threads: 1
 

>Is there a way to paste it?

That's my usual preferred way. I'll flip over to my editor, Ctrl-A to "Select All" of the file, Ctrl-C to copy; flip back to the message editor and Ctrl-V paste it right smack in the middle of [code][/code]. YMMV.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You