| | |
passing function as a parameter to another function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
Hi all,
Do u have any idea that whether I can pass any function as a parameter to another function?
like:
What do you think is this correct (it gave lots of compiling error)...If not is there any other method to do such thing?
Do u have any idea that whether I can pass any function as a parameter to another function?
like:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<vector> using namespace std; int i=5,j=8; int add() { int c=i+j; return c; } int sub() { int c=j-i; return c; } int try(int *add) { if(add()>10) try(sub); } int main() { cout<<try; }
C++ Syntax (Toggle Plain Text)
int try(int *add) { if(add()>10) try(sub); }
C++ Syntax (Toggle Plain Text)
int try(int (*fn)() ) { if(add()>10) try(sub); }
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.
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
hey I tried this one also:
but i got following compile errors
bekar.cpp is filename)
What is wrong with the compiler????
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<vector> using namespace std; int i=5,j=8; int add() { int c=i+j; cout<<c; return c; } int sub() { int c=j-i; cout<<c; return c; } int try(int (*fn)() ) { if(add()>10) try(sub); } int main() { try(add); return 0; }
bekar.cpp is filename) C++ Syntax (Toggle Plain Text)
bekar.cpp:19: error: expected unqualified-id before "try" bekar.cpp: In function `int main()': bekar.cpp:26: error: expected `{' before '(' token bekar.cpp:26: error: expected `catch' before '(' token bekar.cpp:26: error: `add' is not a type bekar.cpp:26: error: invalid catch parameter bekar.cpp:26: error: expected `{' before ';' token bekar.cpp:28:2: warning: no newline at end of file
•
•
Join Date: Apr 2008
Posts: 14
Reputation:
Solved Threads: 3
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.
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.
All the above, plus: try is a keyword in c++ (try...catch) so you might want to rename the function to something else.
•
•
Join Date: Mar 2008
Posts: 41
Reputation:
Solved Threads: 7
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;
}
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;
}
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
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?
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?
•
•
Join Date: Mar 2008
Posts: 41
Reputation:
Solved Threads: 7
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
1,, http://www.newty.de/fpt/index.html
2, http://www.cprogramming.com/tutorial...-pointers.html
![]() |
Similar Threads
- Need a way to make 1 function accessible to all (Python)
- passing reference parameter- homework (C++)
- seek function (Perl)
- passing parameter to Store Procedure in SqlDataAdapter (VB.NET)
- Having trouble passing bool type to function....need help (C++)
- Passing a Function, function pointer (C++)
- Global Buffer Help !!!!!!!!! (C)
- help with questions (Java)
- Is ifs is a member function of ifstream class (C++)
- Data Abstraction (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: MFC and FormView
- Next Thread: Method calling
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






