Recursion and pre-reduction operator
I have this function:
void m(int n)
{
if(n > 0)
m(--n);
cout << n << " "; //for n = 5 --> output is: 0 0 1 2 3 4
}
I have a problem understanding how this works. For instance:
n (input) = 5
output: 0 0 1 2 3 4
My question is: Why does it show zero twice?
When I add the brackets as follows:
void m(int n)
{
if(n > 0)
{
m(--n);
cout << n << " "; // now, it shows 0 1 2 3 4 (n = 5)
}
}
So, what brackets cause in this code that "0" exists only once?
And when I change the pre-decrement (-n) to the post-decrement (n--), it shows nothing. Why?
Can someone help me understand how this works?
+4