Search code examples
recursiontail-recursion

what is front end recursion?


I have seen the term as opposed to tail end recursion and I was wondering what the difference between the two was. So basically What is Front End Recursion?


Solution

  • Front end recursion is when you make the recursive call first in the method, while tail end recursion is when you make the recursive call last in the method.

    Example of front end recursion:

    void Show(int num) {
      if (num > 0) {
        Show(num - 1);
      }
      Console.WriteLine(num);
    }
    

    Result of Show(3);:

    0
    1
    2
    3
    

    Example of tail end recursion:

    void Show(int num) {
      Console.WriteLine(num);
      if (num > 0) {
        Show(num - 1);
      }
    }
    

    Result of Show(3);:

    3
    2
    1
    0