-2

I am getting the output ' 0,1,2,0'. Please someone help me how this function call is working. I have this doubt that after decrementing n, if statement will not execute as it will not be greater than 0. Thus there will be no output. But the result is something else.

#include <stdio.h>

void fun(int);

int main(void)
{
    int a=3;
    fun(a);
    return 0;
}

void fun(int n)
{
    if(n > 0)
    {
        fun(--n);
        printf("%d,", n);
        fun(--n);
    }
}
Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
  • 2
    Get a piece of paper and write out the call sequence. Given that `a` is small, it'll be easy. – ForceBru Mar 25 '17 at 16:41
  • write a few more `printf` statements. ex: before `if` block write one more statement to know the value of `n` initially in that call. – Cherubim Mar 25 '17 at 16:42
  • No disrespect meant but do you know what recursion is? If not, you might want to read up on it for instance at https://en.wikipedia.org/wiki/Recursion_(computer_science) . If you do know, please add that you do know what recursion is to your question so we know to focus on other aspects of the question. – Louis Langholtz Mar 25 '17 at 16:53
  • The `fun(--n)` prints its value before your `fun(a)` – monster Mar 25 '17 at 17:08

1 Answers1

0

You're calling fun() from within itself, both before and after the printf(). So you're going to see the printf from the deepest invocation of fun() first.

@ForceBru is absolutely right, you can - and, if you want to be a programmer, need to be able to - trace the code out in your head or on paper. Here's an example.

on the first invocation of fun(), as is 3. 3 > 0 so fun() is called again, with the _pre_decremented value ( so n is set to 2, and 2 is passed to fun().

The whole process happens again. 2 > 0 so --n or 1 is passed to fun.

In the 3rd invocation deep, n is 1. That's still greater than 0, so fun(--n) is run again, with n being predecremented to 0.

Now within the fourth invocation of fun(), n is finally 0. Nothing happens.

Third invocation, first fun() returns. Rember, this fun() invocation's n was 1, but it's been decremented to 0 by this time. 0 is printed. n is then decremented again ( to -1, it being a signed int) and fun(-1) is called, but since -1 > 0 is false, nothing happens. The third invocation returns.

The second invocation was passed 2 but has since decremented to 1. 1 is printed and then fun(--n) turns into fun(0) and the next fun() invcocation isfun(-1)and also does nothing. The second invocation offun()` returns.

Finally, the first invocation of fun(), the one from main, is executing agian. It started with 3, but decremented to 2. A 2 is printed. Then --n, or 1, is passed to fun() one last time. 1>0 so fun() is invoked again, and outputs--1or 0 just like it did the first time it was passed a 1. The first invocation's recursive calls tofun()then finish, and the program finishes, having output a0, 1, 2, 0`.

Do walk through that yourself on paper, as recursive functions can be pretty confusing. I didn't understand why you got that output myself until I walked through the code, and I've been programing in c for 20 years!

erik258
  • 14,701
  • 2
  • 25
  • 31