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.

Vinesh Chauhan said:   9 years ago
Great logic @Sree and other friends who explain this.

Actually, when first time fun (3); is executed the control transfer to int fun (int n) right.
Then in side of the definition of fun there is another fun() which is fun(--n). So the value of n=2 then again called fun (2) this loop is condition until value of n is equal to 0 then the control passes to printf() because when ever recursion of the function proceeds after that the control return to that point from where it called that's why printf will called value with 0.

Rathika.b said:   1 decade ago
The compiler uses one data structure called stack for implementing normal variables as well as recursion values.

Eg:
int a=3;
printf("%d %d %d",++a,a,a++);

Do you think what is the o/p of this pgm? We think that o/p is: 3 4 5
But it is wrong. Correct o/p is: 5 4 3. Because of stack implementation. similar way you apply this concept into stack. You got correct ans. So, my small opinion is, whenever we see the recursion function, it's simple way to implement stack concept in our mind, for getting solution.

RANEESH said:   1 decade ago
First of all thanks sree for explaining.

Now if we can understand how 0 is printed first then we can understand the whole program.

BRIEFED ANSWER IS IN STEP 3 MUST READ

Firstly if you can figure out this program you can understand above one.

FUNCTION A(INT N)
{
IF(N>0)
{
FUNCTION B() // calls function b
PRINT STATEMENT
}
}

1. Function b is called

2. Function b fails.

3. FUNCTION B RETURNS TO IF STRUCTURE AFTER FAILING AND THEN PRINT STATEMENT EXECUTED and 0 is printed.

4. Then further functions called.

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.

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)

Saidattu G said:   1 decade ago
void fun(int n) is a function
and in this function we again calling fun(--n)

Means it is a recursive function

Here the condition if(n>0) again check

This works as a loop till condition is false

That means till i=0

Then it prints 0

After print again we are calling fun(--n)

Now 2 loops have to execute so

Finally it prints 120.

Aparajita said:   1 decade ago
Whenever the condition fails the it will stop calling the recursive function but it has to come back to the next statement to be executed. In this case after the value of n=0 then it will stop calling fun(--n) but now it will pop the stack and execute the statement next to first fun(--n) function i.e. the printf statement.

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.

Shashi said:   1 decade ago
I will try to explain:

First note that for these things:
1. fun(0) will not execute.
2. fun(1) will only print 0 i.e. --n.

Now follow the tree

fun(2)----> fun(1) -----> print 0
print 1
print 2

fun(1)----> print 0

So output would be 0 1 2 0

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()?
(8)


Post your comments here:

Your comments will be displayed after verification.