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 1 of 10.
Ritesh said:
1 decade ago
Lets change the same program bit differently for understanding the concept properly.....
#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
int a=3;
fun(a); // here the value of a is 3 which is passed to n
return 0;
}
void fun(int n) //here the value obtained is 3 so n=3
{
if(n > 0) //condition is 3>0 hence true, enters the loop
{
fun1(--n); //here value of n = 2 and calls function fun1
printf("%d,", n); //prints 2 after the call function is returned
fun4(--n); //here value again becomes 1
}
}
void fun1(int m) //here the value obtained is 2 so m=2
{
if(m > 0) //condition is 2>0 hence true, enters the loop
{
fun2(--m); //here value of m = 1 and calls function fun2
printf("%d,", m); //prints 1 after the call function is returned
fun5(--m); //here value becomes 0
}
}
void fun2(int p) //here the value obtained is 1 so n=1
{
if(p > 0) //condition is 1>0 hence true, enters the loop
{
fun3(--p); //here value of p = 0 and calls function fun3
printf("%d,", p); //prints 0 after the call function is returned
fun4(--p); //here value again becomes -1
}
}
void fun3(int q) //here the value obtained is 0 so n=0
{
if(q > 0) //condition is 0>0 hence false, func ends and goes back to fun2 func to printf statement.
{
same codes here which are not executed....
}
}
So if u see the values printed here is 0 from fun2 then 1 from fun1 and 2 from fun.... as u can see if the input given to func is 3 it prints 2, if input is 2 then it prints 1 and when input is 1 it prints 0. So when fun2 is printed and the value of p becomes -1 it prints nothing and when it prints 1 from fun1 value of m becomes 0 and prints nothing. But when the value 2 is printed in fun, the value of n becomes 1 so it prints 0 again.
So the output will be :
0,1,2,0,
All the option given in above problem is wrong because, after every no is printed a "," comma should be printed as per the program. But in the option 'D', comma is not given after last zero. In programming even a comma matters a lot.
In the above program all the variables that i declared are taken same and function is called repeatedly instead of calling it with different names like i did.. I just gave diff names so that u will understand the flow. I hope u no the flow of the program when the function is called. I hope u understood the logic. Thank you.
#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
int a=3;
fun(a); // here the value of a is 3 which is passed to n
return 0;
}
void fun(int n) //here the value obtained is 3 so n=3
{
if(n > 0) //condition is 3>0 hence true, enters the loop
{
fun1(--n); //here value of n = 2 and calls function fun1
printf("%d,", n); //prints 2 after the call function is returned
fun4(--n); //here value again becomes 1
}
}
void fun1(int m) //here the value obtained is 2 so m=2
{
if(m > 0) //condition is 2>0 hence true, enters the loop
{
fun2(--m); //here value of m = 1 and calls function fun2
printf("%d,", m); //prints 1 after the call function is returned
fun5(--m); //here value becomes 0
}
}
void fun2(int p) //here the value obtained is 1 so n=1
{
if(p > 0) //condition is 1>0 hence true, enters the loop
{
fun3(--p); //here value of p = 0 and calls function fun3
printf("%d,", p); //prints 0 after the call function is returned
fun4(--p); //here value again becomes -1
}
}
void fun3(int q) //here the value obtained is 0 so n=0
{
if(q > 0) //condition is 0>0 hence false, func ends and goes back to fun2 func to printf statement.
{
same codes here which are not executed....
}
}
So if u see the values printed here is 0 from fun2 then 1 from fun1 and 2 from fun.... as u can see if the input given to func is 3 it prints 2, if input is 2 then it prints 1 and when input is 1 it prints 0. So when fun2 is printed and the value of p becomes -1 it prints nothing and when it prints 1 from fun1 value of m becomes 0 and prints nothing. But when the value 2 is printed in fun, the value of n becomes 1 so it prints 0 again.
So the output will be :
0,1,2,0,
All the option given in above problem is wrong because, after every no is printed a "," comma should be printed as per the program. But in the option 'D', comma is not given after last zero. In programming even a comma matters a lot.
In the above program all the variables that i declared are taken same and function is called repeatedly instead of calling it with different names like i did.. I just gave diff names so that u will understand the flow. I hope u no the flow of the program when the function is called. I hope u understood the logic. Thank you.
Krishan kumar said:
7 years ago
Please, go through function stack calls:-so for given problem here is the solution.
{
please reff to signs as folows
--1-->function call to upper fun();
<--1--return back to upper fun();
--2-->function call to lower fun();
<--2--return back to lower fun();
}
1>from main() :-fun(3) condition is true till as fallows==fun(3)--1-->fun(2)--1-->fun(1)--1-->fun(0) as now n=0 condition is false so the function return to calling function fun(0); with n=0;because if condition is false so nothing to do in that stack fun(0)is completed fun(1)<--1--fun(0).
And execute next statement printf(n=0);
And after that our stack is fun(1)--2-->fun(-1)
Which is again false so nothing to do fun(-1) terminated due to this completion we have executed fun(1) with n=1;
And execute next statement printf(n=1) and with n=1 next statement is fun(0) calling which is again false so nothing to do ;as off now we are done with fun(1).
fun(3)--1-->fun(2)<--1--fun(1) as we return to fun(2) the next statement is printf(n=2) with n=2 we call 2nd one fun(1) which calls again with n=1 so if condition is true calls fun(0) (upper) condition is false so return and execute next statement printf(n=0); so after this execution all the condition is false so we returned to main() function .
fun(3)--1-->fun(2)--1-->fun(1)--1-->fun(0)(false)
print(0)
fun(-1)false both are false return to fun(1)
Next print(1) then next is fun(0) call (false)so fun(1) is complete
fun(3)--1-->fun(2) next is print(2) --2-->fun(1)(true) --1-->fun(0)(false) next is printf(0);after this statement we are just returning to the lower fun() call.
So nothing to print.
{
please reff to signs as folows
--1-->function call to upper fun();
<--1--return back to upper fun();
--2-->function call to lower fun();
<--2--return back to lower fun();
}
1>from main() :-fun(3) condition is true till as fallows==fun(3)--1-->fun(2)--1-->fun(1)--1-->fun(0) as now n=0 condition is false so the function return to calling function fun(0); with n=0;because if condition is false so nothing to do in that stack fun(0)is completed fun(1)<--1--fun(0).
And execute next statement printf(n=0);
And after that our stack is fun(1)--2-->fun(-1)
Which is again false so nothing to do fun(-1) terminated due to this completion we have executed fun(1) with n=1;
And execute next statement printf(n=1) and with n=1 next statement is fun(0) calling which is again false so nothing to do ;as off now we are done with fun(1).
fun(3)--1-->fun(2)<--1--fun(1) as we return to fun(2) the next statement is printf(n=2) with n=2 we call 2nd one fun(1) which calls again with n=1 so if condition is true calls fun(0) (upper) condition is false so return and execute next statement printf(n=0); so after this execution all the condition is false so we returned to main() function .
fun(3)--1-->fun(2)--1-->fun(1)--1-->fun(0)(false)
print(0)
fun(-1)false both are false return to fun(1)
Next print(1) then next is fun(0) call (false)so fun(1) is complete
fun(3)--1-->fun(2) next is print(2) --2-->fun(1)(true) --1-->fun(0)(false) next is printf(0);after this statement we are just returning to the lower fun() call.
So nothing to print.
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.
}
=========================================================
Sree said:
1 decade ago
// typedef int (*pf) (int, int);
// int proc(pf, int, int);
The above two statement has no effects , because they are not used in main or sub function.
Function calls are stored in stack, that is LAST IN FIRST OUT.
1.fun(a=3)---calling sub function--->fun(n=3).
2.fun(3)->if(3>0)true{fun(2)-->step 3,print(n),fun(--n)//active}
3.fun(2)->if(2>0)true{fun(1)-->step 4,print(n),fun(--n)//active}
4.fun(1)->if(1>0)true{fun(0)-->step 5,print(n),fun(--n)//active}
5.fun(0)->if(0>0)false;
6.From step 4,{fun(n=0)//finished,PRINT(n=0),fun(-1)-->step 7}
7.fun(-1)->if(-1>0)false;
8.From step 3,{fun(n=1)//finished,PRINT(n=1),fun(0)-->step 9}
9.fun(0)->if(0>0)false;
10.From step 2,{fun(n=2)//finished,PRINT(n=2),fun(1)-->step 11}
11.fun(1)->if(1>0)true{fun(0)->step 12,print(n),fun(--n)//acti}
12.fun(0)->if(0>0)false;
13.From 11,{fun(n=0)//finished,PRINT(0),fun(-1)-->step 14}
14.fun(-1)->if(-1>0)false;
// all active calls are called and removed from stack.
// here 'n' is a local variable for each function.
// The printing statements are capitalized
PRINT(n=0),PRINT(n=1),PRINT(n=2),PRINT(n=0).
// int proc(pf, int, int);
The above two statement has no effects , because they are not used in main or sub function.
Function calls are stored in stack, that is LAST IN FIRST OUT.
1.fun(a=3)---calling sub function--->fun(n=3).
2.fun(3)->if(3>0)true{fun(2)-->step 3,print(n),fun(--n)//active}
3.fun(2)->if(2>0)true{fun(1)-->step 4,print(n),fun(--n)//active}
4.fun(1)->if(1>0)true{fun(0)-->step 5,print(n),fun(--n)//active}
5.fun(0)->if(0>0)false;
6.From step 4,{fun(n=0)//finished,PRINT(n=0),fun(-1)-->step 7}
7.fun(-1)->if(-1>0)false;
8.From step 3,{fun(n=1)//finished,PRINT(n=1),fun(0)-->step 9}
9.fun(0)->if(0>0)false;
10.From step 2,{fun(n=2)//finished,PRINT(n=2),fun(1)-->step 11}
11.fun(1)->if(1>0)true{fun(0)->step 12,print(n),fun(--n)//acti}
12.fun(0)->if(0>0)false;
13.From 11,{fun(n=0)//finished,PRINT(0),fun(-1)-->step 14}
14.fun(-1)->if(-1>0)false;
// all active calls are called and removed from stack.
// here 'n' is a local variable for each function.
// The printing statements are capitalized
PRINT(n=0),PRINT(n=1),PRINT(n=2),PRINT(n=0).
(2)
IMRAN MOHAMMED said:
1 decade ago
Guys, consider the recursive function (which was called in another function), as a separate new function which was defined in the same manner as the other one. Just try to check the condition at each step and proceed to the next statement. The entire code can be considered as follows :
fun(3)
{
if(3>0)
{
fun(2)
{
if(2>0)
{
fun(1)
{
if(1>0)
{
fun(0)
{
if(0>0)//CONDITION FAILS
}
print(0) //PRINTS 0 FIRST
fun(-1)//CONDITION FAILS
}
}
print(1)//PRINT 1 EXECUTES SECOND
fun(0)// CONDITION FAILS ( 2nd fun(--n) in fun())
}
}
print(2)// PRINT 2 EXECUTES THIRD
fun(1) //( from 2nd fun(n--))
{
if(1>0)
{
fun(0)// CONDITION FAILS
print(0)// PRINT 0 EXECUTES FORTH
fun(-1) // CONDITION FAILS
}
}
}
}
Now just check each condition and the print statement which is executed next. Hope my expalanation is clear.
fun(3)
{
if(3>0)
{
fun(2)
{
if(2>0)
{
fun(1)
{
if(1>0)
{
fun(0)
{
if(0>0)//CONDITION FAILS
}
print(0) //PRINTS 0 FIRST
fun(-1)//CONDITION FAILS
}
}
print(1)//PRINT 1 EXECUTES SECOND
fun(0)// CONDITION FAILS ( 2nd fun(--n) in fun())
}
}
print(2)// PRINT 2 EXECUTES THIRD
fun(1) //( from 2nd fun(n--))
{
if(1>0)
{
fun(0)// CONDITION FAILS
print(0)// PRINT 0 EXECUTES FORTH
fun(-1) // CONDITION FAILS
}
}
}
}
Now just check each condition and the print statement which is executed next. Hope my expalanation is clear.
Raj said:
1 decade ago
Step 1: fun (3) ->3>0.
1.1: fun (2) ->2>0, still lines left to be processed.
1.2: fun (1) ->1>0, still lines left to be processed.
1.3: fun (0) ->0>0, condition fails comes to the next line print the current value of n (i.e. ) n=0; prints 0, next fun (-1) comes out of the current 1. 3 loop.
Now again going back to unprocessed lines in step 1.2,
1.2: Unprocessed line here in this call starts from printf. For this loop n value is 1; prints 1, & comes to next line.
fun (--n) ->fun (0) 0>0 fails. 1.2 loop ends here.
Now again going back to unprocessed lines in step 1.1,
1.1: Unprocessed line here in this call starts from printf. For this loop n value is 2; prints 2, & comes to next line.
fun (--n) ->fun (1) ; once again calling the function fun (1).
1st line - decremented the value to 0;
2nd line prints the value; (prints 0,).
3rd line - fun (-1) totally terminates everything.
Final o/p : 0, 1, 2, 0.
1.1: fun (2) ->2>0, still lines left to be processed.
1.2: fun (1) ->1>0, still lines left to be processed.
1.3: fun (0) ->0>0, condition fails comes to the next line print the current value of n (i.e. ) n=0; prints 0, next fun (-1) comes out of the current 1. 3 loop.
Now again going back to unprocessed lines in step 1.2,
1.2: Unprocessed line here in this call starts from printf. For this loop n value is 1; prints 1, & comes to next line.
fun (--n) ->fun (0) 0>0 fails. 1.2 loop ends here.
Now again going back to unprocessed lines in step 1.1,
1.1: Unprocessed line here in this call starts from printf. For this loop n value is 2; prints 2, & comes to next line.
fun (--n) ->fun (1) ; once again calling the function fun (1).
1st line - decremented the value to 0;
2nd line prints the value; (prints 0,).
3rd line - fun (-1) totally terminates everything.
Final o/p : 0, 1, 2, 0.
(2)
Mahesh Vanol said:
1 decade ago
fun(3) //upside fun() running
if(3>0)
fun(2) //upside fun() running
if(2>0)
fun(1) //upside fun() running
if(1>0)
fun(0) //upside fun() running
if(0>0) false
so return; //upside fun() finished
"print == 0"
fun(-1) //Now downside fun() running
if(-1>0) false
so return; //Downside fun() finished
"print == 1" //BCoz Inside the fun(1) block fun(0)
if(0>0) false
so return; //Downside fun() finished
"print == 2" //BCoz Inside the fun(2) block fun(1)
if(1>0)
fun(0) false
so return;
"print == 0" //BCoz Here last n=0
fun(-1)
if(-1>0) false
so return; //Downside fun() finished
OUTPUT == 0,1,2,0
if(3>0)
fun(2) //upside fun() running
if(2>0)
fun(1) //upside fun() running
if(1>0)
fun(0) //upside fun() running
if(0>0) false
so return; //upside fun() finished
"print == 0"
fun(-1) //Now downside fun() running
if(-1>0) false
so return; //Downside fun() finished
"print == 1" //BCoz Inside the fun(1) block fun(0)
if(0>0) false
so return; //Downside fun() finished
"print == 2" //BCoz Inside the fun(2) block fun(1)
if(1>0)
fun(0) false
so return;
"print == 0" //BCoz Here last n=0
fun(-1)
if(-1>0) false
so return; //Downside fun() finished
OUTPUT == 0,1,2,0
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.
Aayush Gajjar said:
2 years ago
@All.
Although It happens simultaneously, Let's understand the concept as per pre-decrement.
There is --n so first n becomes n-1 and then it goes into the recursion function....
if(n > 0)
{
fun(--n);
printf("%d,", n);
fun(--n);
}
Explained :
f(3){
n = 2
f(2){
n = 1
f(1){
n = 0
f(0){
return to f(1)
{
as per value of n of above f(1)
print(0) -> 0
return to f(2)
{
as per value of n of above f(2)
print(1) -> 1
now, n=0
f(0){
return to f(3){
as per value of n of above f(3)
print(2) -> 2
now, n=1
f(1){
n=0
f(0){
return to f(1){
as per recent value of f(1){
print(0) -> 0
now, f(0){
return
return from f(3).
So, the final answer series is 0, 1, 2, 0.
Although It happens simultaneously, Let's understand the concept as per pre-decrement.
There is --n so first n becomes n-1 and then it goes into the recursion function....
if(n > 0)
{
fun(--n);
printf("%d,", n);
fun(--n);
}
Explained :
f(3){
n = 2
f(2){
n = 1
f(1){
n = 0
f(0){
return to f(1)
{
as per value of n of above f(1)
print(0) -> 0
return to f(2)
{
as per value of n of above f(2)
print(1) -> 1
now, n=0
f(0){
return to f(3){
as per value of n of above f(3)
print(2) -> 2
now, n=1
f(1){
n=0
f(0){
return to f(1){
as per recent value of f(1){
print(0) -> 0
now, f(0){
return
return from f(3).
So, the final answer series is 0, 1, 2, 0.
(22)
Shailendra said:
1 decade ago
fun(3)-1. fun(2)----1.fun(1)-----1.fun(0)
2. printf 2 2.printf 1 2.printf 0
3. fun(1) 3.fun(0) 3.fun(0)
So at first 1. step will execute. fun(3)->fun(2)->fun(1)->fun(0)=0
2.pf 0 -> 3. fun(0)=0 so print 0
Then it will return last statement fun(1).... 1. completed
2. pf 1
3.fun(0)
Now it will return at fun(2)-- 1. completed
2. pf 2
3. fun(1)-->1.fun(0)
2.pf 0
3.fun(0)
2. printf 2 2.printf 1 2.printf 0
3. fun(1) 3.fun(0) 3.fun(0)
So at first 1. step will execute. fun(3)->fun(2)->fun(1)->fun(0)=0
2.pf 0 -> 3. fun(0)=0 so print 0
Then it will return last statement fun(1).... 1. completed
2. pf 1
3.fun(0)
Now it will return at fun(2)-- 1. completed
2. pf 2
3. fun(1)-->1.fun(0)
2.pf 0
3.fun(0)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers