Recursion breaks a problem into smaller instances of itself. Tail recursion occurs when the recursive call is the last operation; some compilers optimize it to iteration, avoiding stack overhead. Understanding when to use recursion vs. iteration, and how to structure recursive calls, is fundamental to algorithm design.
Implement classic recursive algorithms: factorial, fibonacci, tree traversal. Trace the call stack by hand to see growth. Compare recursive and iterative versions of the same function. Experiment with tail-recursive functions and observe stack usage in a language with tail-call optimization (Scheme, some functional languages).
Recursion is a function calling itself to solve a smaller version of the same problem. You already know the basics of algorithm design — breaking problems into steps, defining inputs and outputs. Recursion adds a powerful structural idea: instead of explicitly looping, you define a base case (the simplest version of the problem with a known answer) and a recursive case (how to reduce the current problem to a smaller one). Computing factorial illustrates this cleanly: factorial(1) = 1 (base case), and factorial(n) = n × factorial(n−1) (recursive case). Each call waits for the smaller call to return, then multiplies.
That "waiting" is the critical detail. Every recursive call adds a stack frame — a block of memory holding the function's local variables and return address. For factorial(5), five frames stack up before any of them can return. For factorial(100000), you get 100,000 frames, which will overflow the call stack in most languages. This is the fundamental cost of recursion: each pending call consumes memory proportional to the recursion depth.
Tail recursion is a special pattern where the recursive call is the very last operation the function performs — there is nothing left to do after the recursive call returns. Compare two versions of factorial: the standard version computes `n * factorial(n-1)`, which means it must wait for the recursive result and then multiply — that multiplication happens *after* the recursive call, so the frame must be kept around. A tail-recursive version passes an accumulator parameter: `factorial(n, acc) = factorial(n-1, n*acc)`, with base case `factorial(0, acc) = acc`. Here the recursive call is the final action — no multiplication follows it.
Why does this matter? When the recursive call is truly the last operation, the current stack frame is no longer needed — there is nothing to come back to. A compiler that recognizes this can perform tail-call optimization (TCO): instead of pushing a new frame, it reuses the current one, effectively converting the recursion into a loop. The function runs in constant stack space regardless of recursion depth. Scheme and many functional languages guarantee TCO. Some languages like Java and Python do not, meaning tail recursion in those languages still consumes stack frames. In practice, if your language doesn't support TCO, you can manually convert tail-recursive functions into loops with an accumulator variable — the transformation is mechanical and always possible.