The reason is that each invocation of printNum will print out its own value after the if statement.
Code:
printnum(1) prints "1", calls printnum(1 + 1), does not call cout after the if yet.
printnum(2) prints "2", calls …
(…)
printnum(8) "8"
printnum(9) "9", if (n < 9) == false, so recursion stops. Prints another "9" at the end of its body, returns to printnum(8) by which it was invoked.
printnum(8) "8" at the end of its body.
printnum(7) "7" at the end of its body.
…etc.
p.s. In the future, please tidy up your code a bit for others to read, e.g.:
Code:
#include <iostream>
using namespace std;
void printNum(int num)
{
cout << num; // 1, 2, 3 … 9
if (num < 9)
{
printNum(num + 1);
}
cout << num; // 9, 8, 7 … 1
}
int main()
{
printNum(1);
}
This also makes it more sensible to refer to line numbers when discussing code.