-5

I was in C today and I saw this example of using recursive calls and I don't understand how it works. Can someone please explain?

int print(int nb)
{
    if (nb < 0) 
    {
        return (0);
    }
    printf("%d", nb + print(nb - 1));
    nb --;
    return (nb);
}

int main(void)
{
    print(4);
    return (0);
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Afebu
  • 1
  • 3
    Can you explain why you think it should give `0013610` as the output? – Retired Ninja May 04 '22 at 15:46
  • 1
    The best way to understand recursion is to run your code line by line in a [debugger](https://stackoverflow.com/q/25385173/12149471), while monitoring the [call stack](https://en.wikipedia.org/wiki/Call_stack) and the values of all variables. – Andreas Wenzel May 04 '22 at 15:46
  • 1
    I removed [unix] and [assembly] tags, as they seem irrelevant to this. This is portable C (as long as implicit declaration of printf works without `#include `), and doesn't depend on being run on Unix. And unless I'm missing some C undefined behaviour, its behaviour can be explained without looking at how it compiles to assembly. **Single-step it in a debugger,** with a watch / display set on `nb`. – Peter Cordes May 04 '22 at 15:46
  • 1
    Start simple. What would it print for `print(0)` ? For `print(1)`? And so on. – Eugene Sh. May 04 '22 at 15:50
  • I suggest that you split the line `printf("%d", nb + print(nb - 1));` into two lines: `int temp = print(nb - 1); printf("%d", nb + temp );` That way, the behavior of your program will be much clearer when running it line by line in a debugger, as you won't have multiple things happening in a single line. – Andreas Wenzel May 04 '22 at 15:54
  • @EugeneSh. The confusing part is the line with the `nb --;` – Afebu May 04 '22 at 16:00
  • 1
    What is confusing about it? You don't know what the `--` operator does? – Eugene Sh. May 04 '22 at 16:00
  • `nb --;` is equivalent to `nb = nb - 1;` – Andreas Wenzel May 04 '22 at 16:03
  • @EugeneSh. I know it is supposed to reduce the value of the variable. But the code has this `nb - 1` already. Does that mean the reduction happens before of after that line or it happens after the recursion. – Afebu May 04 '22 at 16:07
  • Both the recursive call parameter and the return value are the original `nb` minus one. You can easily track it down. You could put `nb--` before the `printf` line, and then remove the `-1` from the recursive call (and add `+1` to the printed result) and get the same result. – Eugene Sh. May 04 '22 at 16:12
  • @RetiredNinja The first value is 4. The function would evaluate it as 4 +print (4-1) which is 4 + print (3). Then print (3) would be 3 + print(2) and so on till the result is less than 0. – Afebu May 04 '22 at 16:14
  • @Afebu print(3) does not return 3+print(2). It prints 3+print(2), then it returns something else. – user253751 May 04 '22 at 16:16
  • Okay @EugeneSh. I would try that out. Thanks. – Afebu May 04 '22 at 16:16
  • The expression `nb - 1` does not change the value of the variable `nb`. The expressions `nb = nb - 1` and `nb--` (which are equivalent) however do change the value of the variable `nb`. – Andreas Wenzel May 04 '22 at 16:26

1 Answers1

2

The confusing part is the line with the nb --;

This is the post-decrement operator. You can read about it here:

https://en.cppreference.com/w/c/language/operator_incdec

It decrements nb (subtracts one from it), then returns the original value of nb. So, if nb is originally 3, it sets nb to 2 and returns 3.

Your code ignores the returned value, so it's effectively equivalent to nb = nb - 1;.

ikegami
  • 367,544
  • 15
  • 269
  • 518