int fun(int(*)());

int main()
{
   fun(main);
   cout<<"in main"<<endl;
   return 0;
}

fun(int (*p)())
{
  cout<<"in fun"<<endl;
  return 0;
}

the output is....

in fun
in main

how and in which manner is "main" being passed in the fun function argument in main function.
does this way of passing automatically mean that a function pointer that returns an integer is being passed into the fun function argument????

how does this program work..
kindly help..
Thnx

Recommended Answers

All 7 Replies

the function fun() takes a funtion(whose return type is int and with no params) pointer as its argument.
And u are sending main to that funcion.
Thats it.
Read on pointer to function.

How about you warp your code into code tags, correctly.

Then comment on each line of code, so we can see what you are thinking.

How about you warp your code into code tags, correctly.

Then comment on each line of code, so we can see what you are thinking.

int fun(int(*)());

int main()
{
fun(main);         // the problem is over here...i cant understand how this main is gettin passed to fun??? is it a pointer to main function??
cout<<"in main"<<endl;
return 0;
}

fun(int (*p)())
{
cout<<"in fun"<<endl;
return 0;
}

the output is....

in fun
in main

how and in which manner is "main" being passed in the fun function argument in main function.
does this way of passing automatically mean that a function pointer that returns an integer is being passed into the fun function argument????

how does this program work..

Think of main as a variable. And your function takes a variable a well.
But the variable is not normal, its a function. A function can be passed to another function like a variable. You just need proper prototype.

Compare :

void Foo(int var); //takes in a int variable
void Foo2( int(*pF)() ); //takes in a int function with no parameters

You can also use typedef to make the function pointer look more
natural.

thanks buddy... :)

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):

[B]inside 1st instance of main:[/B]
call myFunction passing main as a parameter
	[B]inside 1st instance of myFunction:[/B]
	count=1
      	function at funcPtr is called.. (i.e. main!)
         	[B]inside 2nd instance of main:[/B]
		call myFunction passing main as a parameter
           		[B]inside 2nd instance of myFunction:[/B]
             		count=2
             		function at funcPtr is called...
               			[B]inside 3rd instance of main:[/B]
                 		call myFunction passing main as a parameter
                   			[B]inside 3d instance of myFunction:[/B]
                   			count = 3
                   			count is 3, so function at funcPtr is not called
                   			[B]3rd myFunction returns 0[/B]
				[B]3rd main returns 0[/B]
			[B]2nd myFunction returns 0[/B]
		[B]2nd main returns 0[/B]
	[B]1st myFunction returns 0[/B]
[B]1st main returns 0[/B]

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.

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):

[B]inside 1st instance of main:[/B]
call myFunction passing main as a parameter
	[B]inside 1st instance of myFunction:[/B]
	count=1
      	function at funcPtr is called.. (i.e. main!)
         	[B]inside 2nd instance of main:[/B]
		call myFunction passing main as a parameter
           		[B]inside 2nd instance of myFunction:[/B]
             		count=2
             		function at funcPtr is called...
               			[B]inside 3rd instance of main:[/B]
                 		call myFunction passing main as a parameter
                   			[B]inside 3d instance of myFunction:[/B]
                   			count = 3
                   			count is 3, so function at funcPtr is not called
                   			[B]3rd myFunction returns 0[/B]
				[B]3rd main returns 0[/B]
			[B]2nd myFunction returns 0[/B]
		[B]2nd main returns 0[/B]
	[B]1st myFunction returns 0[/B]
[B]1st main returns 0[/B]

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.

Thanx a lot buddy...
i really appreciate ur going to such lengths to explain the concept...

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.