Chapter 45: Recursion

1. What is Recursion? (The Simplest Definition)

Recursion means:

A function that calls itself (directly or indirectly) to solve a smaller version of the same problem.

The key sentence every beginner needs to remember:

“To solve a big problem, I solve a smaller version of the same problem… and trust that the smaller one will eventually solve itself.”

But recursion only works if you have two things:

  1. Base case (the smallest problem that I can solve without calling myself again)
  2. Recursive case (the step where I make the problem smaller and call myself again)

Without a proper base case, recursion never stops → infinite recursion → stack overflow crash.

2. Classic First Example – Factorial (most common teaching example)

Factorial definition (mathematical):

text

Recursive definition (the way recursion thinks):

text

Now the Go code:

Go

3. Step-by-Step Execution (Very Important – Trace It!)

Let’s trace factorial(5) mentally:

text

Call stack visualization (what happens in memory):

text

Every recursive call adds a new layer to the call stack. When base case is reached → stack starts unwinding and multiplies back.

4. Another Classic Example – Fibonacci (very common interview question)

Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, …

Rule:

text

Naive recursive version:

Go

Warning: this version is very slow for large n (exponential time O(2ⁿ)) — we usually use dynamic programming / memoization / iterative version in real code.

5. Real-World & Useful Recursive Patterns in Go

Pattern 1: Tree / directory traversal

Go

Pattern 2: JSON / nested data processing

Go

6. When NOT to Use Recursion in Go (Very Important)

  • Large input size → stack overflow (Go default stack is 1–8 MB depending on version/platform)
  • Deep recursion (thousands of levels) → use iterative solution (loop + stack/queue)
  • Performance-critical code → recursion usually slower + more memory than iteration

Rule of thumb 2025–2026:

Use recursion when:

  • Problem is naturally recursive (tree, graph, divide-and-conquer)
  • Depth is small (< 1000–2000 calls)
  • Code clarity is more important than micro-optimization

Use iteration (for / while) when:

  • Input can be large
  • You want predictable memory usage
  • Performance is critical

7. Quick Practice – Try Writing These

  1. Recursive function power(base, exp int) int → base^exp (base case: exp == 0 → return 1)
  2. Recursive function reverse(s string) string → “hello” → “olleh”
  3. Recursive function countDown(n int) that prints n → 1 → “Blast off!”

Which one felt most natural?

Any part still confusing?

  • How to trace recursive calls step-by-step?
  • Base case vs recursive case – which one is more important?
  • Why recursion sometimes slower than loops?
  • Or ready to see memoization / tail recursion next?

Keep writing small recursive functions — once you really feel how the call stack builds and unwinds, recursion becomes one of the most satisfying tools in programming.

You’re doing really great — keep asking! 💪🇮🇳🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *