Jumping into C++ Recursion Example Question
Will someone please explain the second half of this code sequence: 123456789987654321
I understand the first half (123456789...) but at num=9, it kicks out of the If condition, cout's one last time and then return from the function ... How does the sequence decrease?
Code:
#include <iostream>
using namespace std;
void printNum (int num)
{
// the two calls in this function to cout will sandwich an inner
// sequence containing the numbers (num+1)...99...(num+1)
cout << num;
// While begin is less than 9, we need to recursively print
// the sequence for (num+1) ... 99 ... (num+1)
if ( num < 9 )
{
printNum( num + 1 );
}
cout << num;
}
int main ()
{
printNum( 1 );
}