C Programming - Functions - Discussion
Discussion Forum : Functions - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
int a=3;
fun(a);
return 0;
}
void fun(int n)
{
if(n > 0)
{
fun(--n);
printf("%d,", n);
fun(--n);
}
}
Discussion:
96 comments Page 2 of 10.
Biru said:
9 years ago
Not able to understand it. Can anyone help me?
Insiya said:
8 years ago
The main thing to b noted here is that once the if the condition fails only the inner function is returned the remaining calling functions r still there.
The value of n is 3 initially
Fun(3)
{
3 > 0 true
{ fun(2) is called
Printf(2)
Fun(1)}
}
Now wat is fun(2) value . we need to find it
So fun(2)
{
2>0 true
{ fun(1)
Print(1)
Fun(0) }
Find fun(1)
{ 1>0 true
Fun(0)
Print(0)
Fun (-1)
}
Find fun(0)
{
0>0 false so function returns
}
Substitution starts from the most inner loop so it will start from fun(0).
So by back substitution 0 is printed then function returns to fun(1) call and prints 1 the goes to fun (0) which returns again as fun(0) fails conditions so the function returns to fun(2) call and prints 2 and the fun(1) and wch brings us to fun(0) and prints 0.
The value of n is 3 initially
Fun(3)
{
3 > 0 true
{ fun(2) is called
Printf(2)
Fun(1)}
}
Now wat is fun(2) value . we need to find it
So fun(2)
{
2>0 true
{ fun(1)
Print(1)
Fun(0) }
Find fun(1)
{ 1>0 true
Fun(0)
Print(0)
Fun (-1)
}
Find fun(0)
{
0>0 false so function returns
}
Substitution starts from the most inner loop so it will start from fun(0).
So by back substitution 0 is printed then function returns to fun(1) call and prints 1 the goes to fun (0) which returns again as fun(0) fails conditions so the function returns to fun(2) call and prints 2 and the fun(1) and wch brings us to fun(0) and prints 0.
Khan said:
8 years ago
When 0>0 the if statement fails then how the statements inside it will execute?
Manjunath Rk said:
8 years ago
Good explaination, Thanks @Sree.
Vidya said:
8 years ago
Well said @Sree. Thanks.
Fenil said:
8 years ago
Can anyone explain in brief?
As each time function is called & control is passed to function how printf statement will be executed?
As each time function is called & control is passed to function how printf statement will be executed?
Manu said:
8 years ago
I got it as 0,1,0,2,0,1,0.
How? Please tell me.
How? Please tell me.
Abhijit Dey said:
8 years ago
Good explanation, Thanks @Sree.
Surya mitthu said:
9 years ago
I don't understand this. Please someone explain this to me.
Vinesh Chauhan said:
9 years ago
Great logic @Sree and other friends who explain this.
Actually, when first time fun (3); is executed the control transfer to int fun (int n) right.
Then in side of the definition of fun there is another fun() which is fun(--n). So the value of n=2 then again called fun (2) this loop is condition until value of n is equal to 0 then the control passes to printf() because when ever recursion of the function proceeds after that the control return to that point from where it called that's why printf will called value with 0.
Actually, when first time fun (3); is executed the control transfer to int fun (int n) right.
Then in side of the definition of fun there is another fun() which is fun(--n). So the value of n=2 then again called fun (2) this loop is condition until value of n is equal to 0 then the control passes to printf() because when ever recursion of the function proceeds after that the control return to that point from where it called that's why printf will called value with 0.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers