Process, tool, mechanisms that calls itself

Sometimes a big problem can be broken into smaller versions of the same problem — and that’s where recursion shines.

Recursion helps us break down a complex problem into easier sub-problems, until we hit something trivial we can solve.

How to stop a recursive fuction?

The base case is the condition that stops the recursion.

“When the problem becomes simple enough, don’t call myself again — just return an answer.”

long fact(int n) {
	if (n == 1) return 1;     // 👈 base case (stop here)
	return n * fact(n - 1);   // 👈 recursive case (call again)
}

Structure

  1. Base case — the simplest situation (stopping point)
  2. Recursive case — where it calls itself on a smaller version of the problem

E.g. Factorial

fact(3)
→ calls fact(2)
→ calls fact(1)
→ returns 1  ✅ (base case reached)
fact(2) = 2 * 1 = 2 
fact(3) = 3 * 2 = 6

if there’s no base case, the function keeps calling itself forever, creating more and more function calls in memory.

That’s called a stack overflow — literally the program’s call stack “overflows” because it never gets a chance to return.

“Divide and Conquer”