› Forums › Lua › Understanding a Common Recursion Misconception: Does the Parameter n Keep Changing Inside the Same Function Call?
- This topic is empty.
-
AuthorPosts
-
June 16, 2026 at 9:48 am #6904
While learning recursion in Lua, a learner encountered an important conceptual challenge when studying the factorial function.
The learner understood the following recursive implementation:
function fact(n) if n == 0 then return 1 else return fact(n - 1) * n end endHowever, a question naturally arose:
If
nis the input parameter, shouldn’t it remain constant? How does it keep becomingn - 1, thenn - 2, and so on?This question reveals one of the most common misunderstandings beginners have when learning recursion.
The Initial Assumption
Many beginners imagine recursion working like this:
fact(5) n = 5 then somehow n = 4 then n = 3 then n = 2In other words, they imagine that a single variable named
nis repeatedly modified inside the same function call.However, that is not what actually happens.
Each Function Call Gets Its Own Parameter
When Lua executes:
fact(5)it creates a function call with:
n = 5Inside that call, Lua encounters:
fact(n - 1)which becomes:
fact(4)This does not modify the original
n.Instead, Lua creates an entirely new function call.
Visualizing the Calls
The process looks like this:
Call #1 fact(5) n = 5creates:
Call #2 fact(4) n = 4which creates:
Call #3 fact(3) n = 3which creates:
Call #4 fact(2) n = 2which creates:
Call #5 fact(1) n = 1which creates:
Call #6 fact(0) n = 0The important observation is:
The original n never changes.Instead, new function calls are created with new parameter values.
A Helpful Analogy
Imagine a photocopy machine.
The original function definition is:
function fact(n) ... endWhen:
fact(5)is called, Lua effectively creates a working copy:
Function Call #1 n = 5When that call requests:
fact(4)Lua creates another copy:
Function Call #2 n = 4The first copy still contains:
n = 5Nothing was modified.
A completely new function call was created.
A Simpler Example
Consider:
function show(n) print(n) if n > 0 then show(n - 1) end end show(3)Output:
3 2 1 0Many beginners assume that the same variable is repeatedly changing.
In reality, Lua creates:
show(3) show(2) show(1) show(0)Each function call receives its own value of
n.Where Are These Values Stored?
The learner later discovered that every function call is stored separately on the call stack.
Conceptually, the stack looks like:
fact(0) fact(1) fact(2) fact(3) fact(4) fact(5)Each entry contains its own:
nvalue.
This is why multiple versions of
ncan exist simultaneously.The Key Insight
The learner eventually realized that recursion does not repeatedly modify a single variable.
Instead:
- The current function call keeps its own parameter value.
- A recursive call creates a brand-new function call.
- The new function call receives a smaller parameter.
- The process continues until the base case is reached.
For factorial:
5 ↓ 4 ↓ 3 ↓ 2 ↓ 1 ↓ 0Each value belongs to a different function call.
Key Takeaway
One of the most important lessons in recursion is understanding that function parameters do not magically change inside a single call. Every recursive invocation creates a new function call with its own independent copy of the parameter values.
Once this distinction becomes clear, the behavior of recursive functions becomes much easier to understand, and concepts such as call stacks, stack frames, and recursive problem solving begin to make much more sense.
-
AuthorPosts
- You must be logged in to reply to this topic.
