Discussion :: Functions - Find Output of Program (Q.No.18)
|Bagesh Kumar Bagi said: (Dec 1, 2010)|
|First fun(main) come in the function and print the HELLO after that it will be return and it print the Hi
So that the finaly output is
|Jitu said: (Jan 28, 2011)|
|How? please explain with logic.|
|Vinod said: (Mar 22, 2011)|
|I think it is a function pointer.|
|Kunal said: (Mar 26, 2011)|
|Yes it is a function pointer....fun(main) sends a copy of main() function to p and it becomes the argument of fun().it prints hello then return the control to the calling fuction ...then rest of the part is executed.|
|Manu said: (Jun 24, 2011)|
|But what is the use of passing the copy of main() to the function?|
|Ashish said: (Aug 7, 2011)|
|Yes, I agree with Manu.
What is the use of function pointer in this program?
|Praveen said: (Aug 11, 2011)|
|Its a pointer function where p is the reference to call the main().
But here function is not referred by p because p is not initialized to the main().
So only its just act as ordinary function parameter and prints 'Hello Hi'.
|Ria said: (Aug 18, 2011)|
|Please explain more.|
|Riyaz said: (Sep 20, 2011)|
|Can anyone provide explanation to this?|
|Sujith said: (Oct 1, 2011)|
|They did not use the p anywhere in the fun just they printing the statement!|
|Kiran said: (Oct 3, 2011)|
In this (int(*)()) means:
It is a function pointer which returns ineger datatype.
Here the pointer points main function. Main function return 0.
Sa our sub function argument is zero. Then sub function will execute and print Hello.
After that is goes to next line and print hai.
Then main() will be terminate.
|Dinesh said: (Jan 1, 2012)|
|It is function pointer. Here we are initializing the funtion pointer to the address of the main during function call. Then enter the body of the function. Print "hello" then closing braces (}) comes, control back to the calling funtion then prinf"hi" so answer is hellohi.|
|Narendra said: (Mar 18, 2012)|
|Everyone just explaining therotically. Please explain in deep and practically line by line.|
|Govind said: (Jul 3, 2012)|
For those who are thinking what is the use of passing pointer to the main function.
Well, please don't confuse your self.
The function 'int fun (int (*p)())' is a function which takes a function pointer as an argument and returns an integer. And '*p' is a pointer to such a function which takes no argument but return int. Just like our main function.
Now, when the programme starts, main will call fun passing the address of main function. But in the function 'fun' we are not using the address of the main at all. We are just passing it.
So in fun it will only print 'Hello' and return to the calling function where it prints 'Hi' and terminates.
I hope this helps.
|Jhunu said: (Aug 19, 2012)|
|Raju said: (Sep 7, 2012)|
|If I passs any arguments on calling function then how will the function work.|
|Tinky said: (May 10, 2013)|
|Adding to @Raju comment:
If I pass any arguments on the calling functions then how will the function work and what is the use of it?
Passing a variable (by value or reference) has practical implementations in Projects, but what is the use of passing a function? Can we access the variable of function if we pass them as argument?
Thanks in advance,
|Radhika said: (Sep 8, 2013)|
|We can access the variable of function. I think that is the advantage of passing a function as argument to another function.|
|Shravana said: (Dec 20, 2013)|
|Sorry guys. I can't understand the line: int fun(int(*p)()). Can anyone explain it?|
|Xyz said: (Apr 1, 2014)|
|Can we write like these :fun(main) ?
Is it valid? How?
|Asha said: (Apr 24, 2014)|
|What is the meaning of fun(main) ?|
|Robin said: (May 30, 2014)|
|If we are not using the address of main function then what is the need for passing it?|
|Ssfd said: (Aug 21, 2015)|
|The address of main was passed just to complicate things and test whether you can understand the concept or not. In actual program none is going to do so.|
|Govind said: (Apr 10, 2016)|
|Please, can anyone explain fun(main) will not go for infinite?|
|Aritra said: (Aug 17, 2016)|
Your explanations are really helpful. Thanks a lot.
|Robert said: (Aug 18, 2016)|
|Both fun and main are returning integers data types.
int(*p)() = a pointer to a function
Since we can treat a function name like a pointer holding its address so we can access it :
int(*p)() = main (where remember int(*p)() is a pointer to a function and main is also a pointer to the address of function main)
The "main" the argument of the fun is not used in any statement here, thereby it goes with the execution of fun() => the body of the fun will be executed untouched => prints Hello => gives back control to the main function via return 0.
In the main function, after the body of fun() got executed and it returns => prints Hi and exits => answer Hello Hi.
|Niharika said: (Jan 8, 2017)|
|As main call go there will recursion and no output.|
|Karthik said: (Sep 12, 2017)|
|Why the infinite loop not exist when main was used?|
|Nagu said: (Oct 12, 2017)|
|Why the infinite loop not exist when main was used?
The infinite loop does not exist because of the sub-function not recursive function so not infinite loop exists.
The sub-function print "hello" then come to the main function. Then print "hi" end the program.
So not confusing the pointer argument that only pass main program address that not used in sub-function.
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.