Recursion By Hand: a 'warm-up' exercise

Warm-up #2

 

The goal for this exercise is to understand how what recursion is, how it operates within the computer (at a mechanical level), and to use that knowledge to successfully predict what a function produces as output.  The longer-term goal is to get you familiar enough with recursion that you can eventually write (and debug, using the skills you’re developing in this exercise) your own recursive code.

 

What you need to do for this exercise: 

  1. Figure out (by hand) what the following method will print.  Feel free to use the 'print this out a bunch of times' trick:
  2. (If you're handing this in for Pre-Class Exercises, simply write the output that you predict into comments in the class named Warmup_Number2)

namespace RecursionExercises
{
      class Class1
      {
            [STAThread]
            static void Main(string[] args)
            {
                  PrintHelper ph = new PrintHelper();
                  Console.WriteLine( "End result: " + ph.foo(5));
            }
      }
      class PrintHelper
      {
            public int foo(int f)
            {
                  if (f >= 8)
                        return f;
                  Console.WriteLine( "Handed " + f );
                  int i;
                  if(f % 2 == 0)
                       i = foo(f + 2);
                  else
                       i = foo(f + 1);

                  Console.Write( "the return value is: " + i);
                  return i;
           }
      }
}