Need help on thinking recursively

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;

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