Need help on thinking recursively

Dec 10, 2014 at 2:22am
I just wrote a function recursively so I can understand how recursion works, but the logic in this still escapes me. Why does this function display 55 when it should display 19, 18, 17, 16 ... 0?

1
2
3
4
5
6
7
8
9
int count (int num)
{
   if (num <= 0)
        return 0;

   else
        return count (num - 1) + num;

}
Dec 10, 2014 at 4:48am
Do you want it to print out all the numbers from num to 0?

instead of returning count ( num- 1 ) + num, you should print out num and then return count (num - 1)
Dec 10, 2014 at 5:17am
Why does this function display 55 when it should display 19, 18, 17, 16 ... 0?

The function in question does not display anything.
Topic archived. No new replies allowed.