title: Tail recursion, how do we eliminate the return statement?
body: <p>Here is a tail recursive C# program that sums the squares of 1 through 10. It works, except for the last else  in <code>AddSquares2()</code>.  It does not work because C# seems to demand a return statement in every path in a method that returns a value. However, I shouldn't need a return statement in a tail recursive method such as this one. See the comment.</p>
  
  <p>Is this just an idiosyncrasy of C#? Or is there a way to tell the compiler that I know what I'm doing and that I don't want every path to return a value?</p>
  
  <p>Here is my program:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace Recursive
  {
      class Program
      {
          static void Main(string[] args)
          {
              int start = 1;
              int end = 10;
              int sum = 0;
              AddSquares(start, end, sum);
              sum = AddSquares2(start, end, sum);
          }
  
          private static int AddSquares2(int start, int end, int sum)
          {
              Console.WriteLine($"AddSquares2({start}, {end}, {sum})");
              if (start &gt; end)
                  return (sum);
              else
                  //should not have to return anything here
                  return(AddSquares2(start + 1, end, sum + start * start));
          }
  
          private static void AddSquares(int start, int end, int sum)
          {
              Console.WriteLine($"AddSquares({start}, {end}, {sum})");
              if (start &gt; end)
                  Console.WriteLine($"  The total sum is {sum}");
              else
                  AddSquares(start + 1, end, sum + start * start);
          }
      }
  }
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code> AddSquares(1, 10, 0)
      AddSquares(2, 10, 1)
      AddSquares(3, 10, 5)
      AddSquares(4, 10, 14)
      AddSquares(5, 10, 30)
      AddSquares(6, 10, 55)
      AddSquares(7, 10, 91)
      AddSquares(8, 10, 140)
      AddSquares(9, 10, 204)
      AddSquares(10, 10, 285)
      AddSquares(11, 10, 385)
        The total sum is 385
      AddSquares2(1, 10, 0)
      AddSquares2(2, 10, 1)
      AddSquares2(3, 10, 5)
      AddSquares2(4, 10, 14)
      AddSquares2(5, 10, 30)
      AddSquares2(6, 10, 55)
      AddSquares2(7, 10, 91)
      AddSquares2(8, 10, 140)
      AddSquares2(9, 10, 204)
      AddSquares2(10, 10, 285)
      AddSquares2(11, 10, 385)
        The total sum is 385
  </code></pre>