Hi all,
Do u have any idea that whether I can pass any function as a parameter to another function?
like:

#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;
}

What do you think is this correct (it gave lots of compiling error)...If not is there any other method to do such thing?:-/

int try(int *add)
{
	if(add()>10)
           try(sub);
}

That does work because the parameter to try() is not a pointer to a function. This should fix the compile error.

int try(int (*fn)() )
{
	if(add()>10)
           try(sub);
}

hey I tried this one also:

#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;
}

but i got following compile errors:(bekar.cpp is filename)

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

What is wrong with the compiler????:S

btw thanx for your fast reply.

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.

All the above, plus: try is a keyword in c++ (try...catch) so you might want to rename the function to something else.

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;
}

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????:-O
but the function is made at run time...correct me if I am wrong?:$
:?: :?: :?: :?: :?: :?: :?: :?: :?: :?: :?: :?:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.