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 8 of 10.
Khan said:
8 years ago
When 0>0 the if statement fails then how the statements inside it will execute?
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.
Asha said:
8 years ago
Thank you guys. I understood stack.
Kowsik said:
8 years ago
When fun(0) is called 0>0 condition fail and it will not enter in the loop further. So the program will not provide any output. Correct me if I am wrong.
BhavadipGothadiyaBG said:
8 years ago
How it's Possible?
Tushar said:
8 years ago
How does calling function works ?
Naresh said:
8 years ago
I am not getting what happens after f (0). How will the output be 0 1 2 0?
Sandy said:
8 years ago
@ALL. According to me.
void f(3){
if(3 > 0)
{
f(2) //the first fun(--n) // ((when you are on this line directly go to f(2) and if that executed then execute the below line))
print(2);
f(1)//the second fun(--n)((when you are on this line directly go to f(1) and if that executed then execute the below line))
}
}
=========================================================
void f(2){
if(2 > 0)
{
f(1) // ((when you are on this line directly go to f(1) and if that executed then execute the below line))
print(1);
f(0) // ((when you are in this line directly go to f(0) and if that executed then execute the below line))
}
}
=========================================================
void f(1){
if(1 > 0)
{
f(0) // ((when you are in this line directly go to f(0) and if that executed then execute the below line))
print(0);
f(-1) // i am not writing this function as it will never be executed because it will not cross if(-1 > 0).
}
}
=========================================================
void f(0)
{
if(0 > 0) { go back simon !!! } //this will never be executed so ever encounter the f(0) remember its nothing.
}
=========================================================
void f(3){
if(3 > 0)
{
f(2) //the first fun(--n) // ((when you are on this line directly go to f(2) and if that executed then execute the below line))
print(2);
f(1)//the second fun(--n)((when you are on this line directly go to f(1) and if that executed then execute the below line))
}
}
=========================================================
void f(2){
if(2 > 0)
{
f(1) // ((when you are on this line directly go to f(1) and if that executed then execute the below line))
print(1);
f(0) // ((when you are in this line directly go to f(0) and if that executed then execute the below line))
}
}
=========================================================
void f(1){
if(1 > 0)
{
f(0) // ((when you are in this line directly go to f(0) and if that executed then execute the below line))
print(0);
f(-1) // i am not writing this function as it will never be executed because it will not cross if(-1 > 0).
}
}
=========================================================
void f(0)
{
if(0 > 0) { go back simon !!! } //this will never be executed so ever encounter the f(0) remember its nothing.
}
=========================================================
Malavika said:
8 years ago
Please, can anyone explain clearly? because after printing 0 then the value becomes -1. After the process, I did not understand.
Shaheen Khan said:
8 years ago
I understood till 0 gets printed someone please explain after printing of 0 where does the call goes and how it works?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers