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 2 of 10.

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

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.

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

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)

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.

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

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?

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.

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.
}
=========================================================

Naresh said:   8 years ago
I am not getting what happens after f (0). How will the output be 0 1 2 0?


Post your comments here:

Your comments will be displayed after verification.