passing function as a parameter to another function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

passing function as a parameter to another function

 
0
  #1
May 15th, 2008
Hi all,
Do u have any idea that whether I can pass any function as a parameter to another function?
like:
  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passing function as a parameter to another function

 
0
  #2
May 15th, 2008
  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.
  1. int try(int (*fn)() )
  2. {
  3. if(add()>10)
  4. try(sub);
  5. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: passing function as a parameter to another function

 
0
  #3
May 15th, 2008
hey I tried this one also:
  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)
  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????
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: passing function as a parameter to another function

 
0
  #4
May 15th, 2008
btw thanx for your fast reply.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 14
Reputation: Abzero is an unknown quantity at this point 
Solved Threads: 3
Abzero Abzero is offline Offline
Newbie Poster

Re: passing function as a parameter to another function

 
1
  #5
May 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,918
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: passing function as a parameter to another function

 
0
  #6
May 15th, 2008
All the above, plus: try is a keyword in c++ (try...catch) so you might want to rename the function to something else.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 41
Reputation: RenjithVR is an unknown quantity at this point 
Solved Threads: 7
RenjithVR RenjithVR is offline Offline
Light Poster

Re: passing function as a parameter to another function

 
1
  #7
May 16th, 2008
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: passing function as a parameter to another function

 
0
  #8
May 16th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 41
Reputation: RenjithVR is an unknown quantity at this point 
Solved Threads: 7
RenjithVR RenjithVR is offline Offline
Light Poster

Re: passing function as a parameter to another function

 
1
  #9
May 19th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC