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 5 of 10.
Bala said:
9 years ago
Thanks, @Ritesh.
Your deep explanation clears all my doubt.
Your deep explanation clears all my doubt.
Ssh said:
1 decade ago
@RAJ explaination is easy to understand.
I would like to know. Is variable n a local variable whose value is flushed at the end of each function call?
Because after printing 0. Next statement is again fun (--n) ; where n becomes -1;
Now -1>0 condition fails.
Now stack points to next statement in function FUN (1) i.e. printf ("%d, ", n);
So why does it print 1 instead of -1.
I would like to know. Is variable n a local variable whose value is flushed at the end of each function call?
Because after printing 0. Next statement is again fun (--n) ; where n becomes -1;
Now -1>0 condition fails.
Now stack points to next statement in function FUN (1) i.e. printf ("%d, ", n);
So why does it print 1 instead of -1.
Debalina said:
1 decade ago
@Vishal and @Sree I don't really get it for step 3.
In step 3 it should not print anything rather exit from the function, so how will zero be printed?
After all the print statement is within the if block?
In step 3 it should not print anything rather exit from the function, so how will zero be printed?
After all the print statement is within the if block?
Vishal said:
1 decade ago
if(3>0) true.
step:1 fun(2).
if(2>0) true.
step:2 fun(1).
if(1>0) true.
step:3 fun(0).
if(0>0) false.
print 0 from step:3, fun(-1) false.
print 1 from step:2, fun(0) false.
print 2 from step:1, fun(1)->fun(0).
print 0 from fun (0) at second last line.
step:1 fun(2).
if(2>0) true.
step:2 fun(1).
if(1>0) true.
step:3 fun(0).
if(0>0) false.
print 0 from step:3, fun(-1) false.
print 1 from step:2, fun(0) false.
print 2 from step:1, fun(1)->fun(0).
print 0 from fun (0) at second last line.
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)
ABHI_027 said:
1 decade ago
How can it be printed as recursive function is executed repeatedly calling & at last the program is ended with false statements?
VIK said:
1 decade ago
PRINT(n=0), PRINT(n=1), PRINT(n=2), PRINT(n=0).
Arnab said:
1 decade ago
You can arrange each function call in a block and then understand it becomes easy,
As fun(3)---->fun(2)----------->
printf(2)
fun(1)
//After encountering parentheses return to the next line of the calling function.
As fun(3)---->fun(2)----------->
printf(2)
fun(1)
//After encountering parentheses return to the next line of the calling function.
Shafeeq said:
1 decade ago
Any simple method to find output of function with recursion?
Rishabh said:
1 decade ago
In step 5 the condition of if gets failed. Means it doesn't enter in its body. So how the output will generate?
Please explain.
Please explain.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers