Recursive Functions

Hi guys. I'm having a hard time trying to understand Recursive Functions.

1
2
3
4
5
6
7
8
9
10
void printnum ( int begin )
{
  cout<< begin;
  if ( begin < 9 )         // The base case is when begin is greater than 9
  {                           //  for it will not recurse after the if-statement
      printnum ( begin + 1 ); 
  }
  cout<< begin;         // Outputs the second begin, after the program has
                              //  gone through and output
}


What I don't understand is the part after the recursive function has been called. Why does it start at 9 and counts back down instead of just displaying 0? I mean it would make a lot more sense if it displayed 0 after the recursive function is called.
Last edited on
Make a step by step run through a debugger
Keep an eye on the call stack
lulz...
This prints 01234567899876543210 doesn't it?
Last edited on
The function takes a parameter by value. Each call of the function has its own local variable named "begin", which value just happens to be initialized to be one more than the value of a variable with same name in the calling instance of the function.
the function will 'pause' when it calls another function, and will 'resume' at the next sentence when the 'another' function finished.
is it the point you are confusing?

say if i call printnum(8)
p8 cout << 8
p8 if (8 < 9) printnum(9)
p9 printnum(9)
cout << 9
cout << 9
p8 cout << 8
Each printnum ( begin + 1 ); has new begin variable...
When the if breaks down each printnum ( begin + 1 ); function executes its second command that is to display begin variable...
@MienTommy

Hi guys. I'm having a hard time trying to understand Recursive Functions.

1
2
3
4
5
6
7
8
9
10
 void printnum ( int begin )
{
  cout<< begin;
  if ( begin < 9 )         // The base case is when begin is greater than 9
  {                           //  for it will not recurse after the if-statement
      printnum ( begin + 1 ); 
  }
  cout<< begin;         // Outputs the second begin, after the program has
                              //  gone through and output
}



It is not difficult to understand the function if to make some modifications. Let remove code snipet

1
2
3
4
  if ( begin < 9 )         // The base case is when begin is greater than 9
  {                           //  for it will not recurse after the if-statement
      printnum ( begin + 1 ); 
  }


We will get

1
2
3
4
5
6
7
 void printnum ( int begin )
{
  cout<< begin;

  cout<< begin;         // Outputs the second begin, after the program has
                              //  gone through and output
}


So if the function is called with begin equal to 0 then the function output is

00

But between these two outputs (that is between two 0) this function calls itself with argument equal to begin + 1 that is equal to 1 due to the code snipet that I removed for clearity.

1
2
3
4
  if ( begin < 9 )         // The base case is when begin is greater than 9
  {                           //  for it will not recurse after the if-statement
      printnum ( begin + 1 ); 
  }



So if to consider the second call of the function with the argument equal to 1 without the internal code snip then we get as in the case above

11

But this output will be placed between the previous two 00. So we will get

0110

and so on.

Last edited on
nice explanation... I was trying to say the same thing :)
Topic archived. No new replies allowed.