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);
    }
}
0, 2, 1, 0,
1, 1, 2, 0,
0, 1, 0, 2,
0, 1, 2, 0,
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
96 comments Page 9 of 10.

Shahid said:   8 years ago
Output is wrong, it's a recursive function.

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.

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
(1)

Guddi said:   7 years ago
I don't understand. Please explain to me.

Nikk said:   7 years ago
If 3>0 then --it will call fun(2) & fun(2) call fun(1)& fun(1) call fun(0)
then fun(0)returns 0 to fun(1),it will print(0), After that how the flows goes?

Please explain it.

Rajarshi said:   7 years ago
Here, we can Apply the concept of recursive tree.

VInith kumar said:   7 years ago
Simply great @Mukunda Saini.

Harish Mahajan said:   7 years ago
How can I recognize a function is a recursion?

Please explain me.
(1)

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
(4)

Tejashri said:   7 years ago
Hi, please explain this program in simple way.


Post your comments here:

Your comments will be displayed after verification.