Tail Recursion
Hello everyone! 👋 Today’s article will be about tail recursion, a technique that allows you to optimize certain recursive functions. Introduction In short, when you write a recursive function, each new call it does allocates a frame onto the stack. For example, let us take this following function: 1 2 3 4 5 6 7 8 private static long RecursiveFib(long n) { if (n <= 1) { return n; } return RecursiveFib(n - 1) + RecursiveFib(n - 2); } If we set a breakpoint at return n and call the function with RecursiveFib(10), we will get the following stack frame. There are 10 entries in the stack frame. ...