943,681 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1156
  • C++ RSS
May 15th, 2008
0

passing function as a parameter to another function

Expand Post »
Hi all,
Do u have any idea that whether I can pass any function as a parameter to another function?
like:
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4.  
  5. using namespace std;
  6. int i=5,j=8;
  7. int add()
  8. {
  9. int c=i+j;
  10. return c;
  11. }
  12. int sub()
  13. {
  14. int c=j-i;
  15. return c;
  16. }
  17. int try(int *add)
  18. {
  19. if(add()>10)
  20. try(sub);
  21. }
  22. int main()
  23. {
  24. cout<<try;
  25. }
What do you think is this correct (it gave lots of compiling error)...If not is there any other method to do such thing?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
May 15th, 2008
0

Re: passing function as a parameter to another function

C++ Syntax (Toggle Plain Text)
  1. int try(int *add)
  2. {
  3. if(add()>10)
  4. try(sub);
  5. }
That does work because the parameter to try() is not a pointer to a function. This should fix the compile error.
C++ Syntax (Toggle Plain Text)
  1. int try(int (*fn)() )
  2. {
  3. if(add()>10)
  4. try(sub);
  5. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,949 posts
since Aug 2005
May 15th, 2008
0

Re: passing function as a parameter to another function

hey I tried this one also:
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4.  
  5. using namespace std;
  6. int i=5,j=8;
  7. int add()
  8. {
  9. int c=i+j;
  10. cout<<c;
  11. return c;
  12. }
  13. int sub()
  14. {
  15. int c=j-i;
  16. cout<<c;
  17. return c;
  18. }
  19. int try(int (*fn)() )
  20. {
  21. if(add()>10)
  22. try(sub);
  23. }
  24. int main()
  25. {
  26. try(add);
  27. return 0;
  28. }
but i got following compile errorsbekar.cpp is filename)
C++ Syntax (Toggle Plain Text)
  1. bekar.cpp:19: error: expected unqualified-id before "try"
  2. bekar.cpp: In function `int main()':
  3. bekar.cpp:26: error: expected `{' before '(' token
  4. bekar.cpp:26: error: expected `catch' before '(' token
  5. bekar.cpp:26: error: `add' is not a type
  6. bekar.cpp:26: error: invalid catch parameter
  7. bekar.cpp:26: error: expected `{' before ';' token
  8. bekar.cpp:28:2: warning: no newline at end of file
  9.  
What is wrong with the compiler????
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
May 15th, 2008
0

Re: passing function as a parameter to another function

btw thanx for your fast reply.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
May 15th, 2008
1

Re: passing function as a parameter to another function

this: int (*fn)() is declaring fn as a pointer to a function which takes no parameters and returns an int.

But in your code you still refer to it as add(), you need to call fn().

Function points are a nice, but can get complex in C++. An other option would be 'Functor' classes (classes which override the () operator) which would do a similar thing.
Reputation Points: 28
Solved Threads: 3
Newbie Poster
Abzero is offline Offline
14 posts
since Apr 2008
May 15th, 2008
0

Re: passing function as a parameter to another function

All the above, plus: try is a keyword in c++ (try...catch) so you might want to rename the function to something else.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 16th, 2008
1

Re: passing function as a parameter to another function

Hi,
You cannot use try as a function name in c++, because try is a keyword in c++ used for exception handling.
The following code should work.

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

int i=5,j=8, n= 10;
int (fn)();

int add()
{
int c=i+j;
cout<<c;
return c;
}
int sub()
{
int c=j-i;
cout<<c;
return c;
}
int ans(int (*fn)() )
{
if( add() )
sub();
}
int main()
{
ans(add);
return 0;
}
Reputation Points: 12
Solved Threads: 7
Light Poster
RenjithVR is offline Offline
41 posts
since Mar 2008
May 16th, 2008
0

Re: passing function as a parameter to another function

so far so gud thanx the program ran....
but I have one question....
pointer to a variable means a thing which points to the variable i.e.it stores the address of the variable...right?
What does pointer to a function imply?
a variable which is pointer to a function stores the address of the function????
but the function is made at run time...correct me if I am wrong?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
shankhs is offline Offline
58 posts
since Jan 2008
May 19th, 2008
1

Re: passing function as a parameter to another function

go through this links. This will clear you all about function pointers.

1,, http://www.newty.de/fpt/index.html
2, http://www.cprogramming.com/tutorial...-pointers.html
Reputation Points: 12
Solved Threads: 7
Light Poster
RenjithVR is offline Offline
41 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: MFC and FormView
Next Thread in C++ Forum Timeline: Method calling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC