› Forums › Lua › Why Does a Recursive Function Keep Repeating Itself Without a Loop? (Lua Learning Q&A)
- This topic is empty.
-
AuthorPosts
-
June 13, 2026 at 9:17 am #6891
While learning recursion in Lua, a learner encountered the following factorial function:
function fact(n) if n == 0 then return 1 else return n * fact(n - 1) end endAt first, the learner understood how loops create repetition:
for i = 1, 5 do print(i) endIn a loop, the repetition is obvious because the code explicitly instructs the program to execute multiple times.
However, the factorial function contains no
forloop and nowhileloop.This raised an important question:
If there is no loop, why does the function keep repeating itself?
The Common Misunderstanding
Many beginners initially assume that a recursive function somehow repeats automatically.
However, functions do not automatically repeat themselves.
For example:
function hello() print("Hello") endCalling:
hello()produces:
Helloonce and then stops.
The function does not repeat because there is no instruction telling it to do so.
A Function Can Call Itself
Now consider the following example:
function hello() print("Hello") hello() endWhen:
hello()is executed, the function reaches:
hello()inside its own body.
This creates a brand-new function call.
The new function call executes the same code and eventually reaches:
hello()again.
This creates yet another function call.
The process continues indefinitely.
The important observation is that the function is not looping. Instead, one function call is creating another function call.
How Factorial Works
In the factorial function, the critical line is:
fact(n - 1)Suppose the learner calls:
fact(5)Lua executes:
return 5 * fact(4)To calculate this result, Lua must first determine the value of:
fact(4)This creates a new function call.
That new function call creates:
fact(3)which creates:
fact(2)which creates:
fact(1)which finally creates:
fact(0)The chain looks like this:
fact(5) ↓ fact(4) ↓ fact(3) ↓ fact(2) ↓ fact(1) ↓ fact(0)Each call creates another call.
No loop is involved.
Recursion Versus Loops
A loop works as follows:
for i = 1, 5 do print(i) endCharacteristics:
- One function call
- Many loop iterations
- The loop controls repetition
Recursion works differently:
fact(5)Characteristics:
- Many function calls
- One execution path per function call
- The function controls repetition by calling itself
Why Doesn’t Recursion Continue Forever?
Without a stopping condition, recursion would continue indefinitely.
For example:
function bad(n) return bad(n - 1) endThis function never stops creating new calls.
Eventually, the program crashes because the call stack becomes full.
The Base Case
The factorial function contains:
if n == 0 then return 1 endThis is called the base case.
When:
fact(0)is reached, no new recursive call is created.
Instead:
return 1is executed immediately.
This allows all the waiting function calls to finish and return their results.
An Important Mental Model
Many beginners picture recursion as:
One function running repeatedlyHowever, recursion is more accurately described as:
One function call creates another function call which creates another function call which creates another function callThe repetition comes from creating new function calls, not from a loop.
Key Takeaway
A recursive function does not repeat because functions magically loop. Instead, each execution explicitly calls the same function again with a smaller problem. This creates a chain of function calls that continues until a base case stops further calls.
The learner eventually realized that loops create repetition inside a single function call, whereas recursion creates repetition by generating many function calls. Understanding this distinction is one of the most important milestones in learning recursion.
-
AuthorPosts
- You must be logged in to reply to this topic.
