dkalita has already explained it to you...Try reading his post again!
The function 'fun' returns an int.
But it takes a pointer to a function as a parameter.
The function pointer must point to a function which returns an int and takes no parameters.
main happens to be a function, it returns an int and it takes no parameters, so you can easily pass a pointer to main into the function. Even from within main!
Function pointers do have their uses and I've never seen a function which took a pointer to main as a parameter, but I guess there may be some practical applications, perhaps some recursive algorithms might call for something like this.
I've expanded on the example you posted. This little doozy uses a function pointer to implement recursion:
#include <iostream>
using namespace std;
// function declaration
int myFunction(int (*funcPtr)());
// global static int to stop us from recursing too far
static unsigned int count=0;
int main()
{
cout << "inside main" << endl;
// now we'll call our function passing a pointer to main
myFunction(main);
cout << "exiting main" << endl;
return 0;
}
// myFunction:
// calls a function pointed to by the input parameter (a function pointer)
// \returns<int>
// \param<funcPtr>takes a pointer to a function which returns an int but takes no parameters
int myFunction(int (*funcPtr)())
{
cout << "inside myFunction!" << endl;
// increment the counter
count++;
// if count is less than 3:
if(count<3)
(*funcPtr)(); //call the function being pointed to
//....so main is called again...
cout << "exiting myFunction" << endl;
return 0;
}
All I've done there, I've given the function and its parameter slightly more meaningful names, I've also added some extra couts.
You'll also note that the function pointed to by funcPtr is called inside myFunction.
I've also added a static int to stop the program from recursing too far (without the limit in there the program would recurse until it ran out of memory and crashed)
So let's see what's happening when you run the above program, (ignoring the extra couts):
inside 1st instance of main:
call myFunction passing main as a parameter
inside 1st instance of myFunction:
count=1
function at funcPtr is called.. (i.e. main!)
inside 2nd instance of main:
call myFunction passing main as a parameter
inside 2nd instance of myFunction:
count=2
function at funcPtr is called...
inside 3rd instance of main:
call myFunction passing main as a parameter
inside 3d instance of myFunction:
count = 3
count is 3, so function at funcPtr is not called
3rd myFunction returns 0
3rd main returns 0
2nd myFunction returns 0
2nd main returns 0
1st myFunction returns 0
1st main returns 0
I hope that hasn't confused you too much, I hope you followed that ok.
Take a look at this function definition:
int anotherFunction(float(*pFunc)(string, int));
This function returns an int and it takes a function pointer as a parameter.
The function pointer in this case takes a pointer to a function which returns a float and takes a string and an int as parameters.
So anotherFunction can be passed a pointer to any function which returns float and takes a string and an int as parameters.
so if we had a function defined like so:
float doSomething(string str, int index);
we can pass a pointer to it into anotherFunction like this:
anotherFunction(doSomething);
And I think that's about it!
Cheers for now,
Jas.