Circularity in Recursion
What is Circularity in Recursion?
In recursion, a function calls itself to solve a smaller version of a problem.
Circularity refers to this self-referential structure.
But — not all circularity is good.
To work properly, recursion must have:
-
A Base Case — a condition where recursion stops
-
A Recursive Case — where the function calls itself with a simpler input
If circularity exists without a base case, the recursion becomes infinite → ❌ stack overflow.
✔️ Good Circularity Example
Computing factorial of a number
Let’s trace factorial(4):
Each call reduces the problem (n decreases), eventually hitting the base case (0).
❌ Bad Circularity Example
Missing base case → infinite loop
This function never stops calling itself and eventually crashes with:
Why is Circularity Important?
Programmers often forget the base case or fail to make progress toward it.
We emphasize:
| Good Circularity | Bad Circularity |
|---|---|
| Has a stopping condition | No base case |
| Moves toward base case | Problem never simplifies |
| Produces a result | Infinite recursion / crash |
Comments
Post a Comment