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.
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)
Sudheer said:
5 years ago
If you use stack concept then question contain stack key.
The Correct answer for this question is, no output. Because if the condition is true then the loop will be created. If the condition is false(n=0)it do not execute if condition. Then how control goes to printf()?
The Correct answer for this question is, no output. Because if the condition is true then the loop will be created. If the condition is false(n=0)it do not execute if condition. Then how control goes to printf()?
(8)
Mohan said:
6 years ago
I have a doubt when the functions are called recursively each time n value decremented by 1 hence finally it comes 0 so now n value in fun (0) is 0 so now n=0 then how it prints 1 2 in fun (1) fun (2) because here also n must be 0 how it happens? Please tell me.
(6)
Anil said:
7 years ago
It is a stack concept.
When n=3 then it becomes 3,2,1,0 as per condition, these values are stored in the stack
0 1 2 3.
When the condition becomes false then automatically values are poped. Here top value is 0 or last inserted so 0 is first poped and execution jumps to print function and display 0 and same for all.
At last output will be;
0 1 2 0
When n=3 then it becomes 3,2,1,0 as per condition, these values are stored in the stack
0 1 2 3.
When the condition becomes false then automatically values are poped. Here top value is 0 or last inserted so 0 is first poped and execution jumps to print function and display 0 and same for all.
At last output will be;
0 1 2 0
(4)
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)
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)
Sanjib Kumar Mandal said:
4 years ago
Thanks all for giving the explanation.
(1)
Hardik said:
6 years ago
@Raj.
Your explanation is the best. Thanks.
Your explanation is the best. Thanks.
(1)
Harish Mahajan said:
7 years ago
How can I recognize a function is a recursion?
Please explain me.
Please explain me.
(1)
Mukunda saini said:
7 years ago
According to me, It is;
fun(3)->fun(2) -> fun(1) -> fun(0)
print 2 print 1 print 0 finish
fun(1)-> fun(0) fun(-1)
print 0 finish finish
finish
fun(3)->fun(2) -> fun(1) -> fun(0)
print 2 print 1 print 0 finish
fun(1)-> fun(0) fun(-1)
print 0 finish finish
finish
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers