- This topic is empty.
-
AuthorPosts
-
June 24, 2026 at 7:47 am #7023
While learning Lua, it is common to encounter code like the following:
function fact(n) if n == 0 then return 1 else return fact(n - 1) * n end end a = io.read("*n") print(fact(a))Many beginners naturally wonder whether the
nappearing in:function fact(n)
must somehow match the
nappearing in:io.read("*n")At first glance, it appears that both uses of
nare connected.In reality, they are completely unrelated.
The First
n: A Function ParameterConsider the function definition:
function fact(n)
Here,
nis simply a parameter name chosen by the programmer.A parameter acts as a temporary variable that receives a value when the function is called.
For example:
fact(5)
causes:
n = 5
inside the function.
The name
nis not special.The programmer could choose almost any valid variable name.
For example:
function fact(y) if y == 0 then return 1 else return fact(y - 1) * y end endThis version works exactly the same way.
The only requirement is consistency.
If the parameter is named
y, then the function body must also usey.
The Second
n: A Lua Format SpecifierNow consider:
io.read("*n")The
nhere has a completely different meaning.It is not a variable.
It is not a parameter.
It is not connected to the function.
Instead, it is part of Lua’s built-in input system.
The expression:
"*n"
is a format specifier that tells Lua:
Read a number from the keyboard.
Therefore:
a = io.read("*n")means:
Read a number entered by the user and store it in variable
a.
Thinking Of
*nAs A Built-In CommandA useful mental model is to think of
*nas a predefined instruction built into Lua.For example:
io.read("*n")means:
Read a Number
Similarly:
io.read("*l")means:
Read a Line
and:
io.read("*a")means:
Read All Remaining Input
These are predefined options recognized by Lua.
Why Changing
*nDoes Not WorkSuppose a programmer writes:
io.read("*y")thinking that the letter should match the function parameter name.
This will fail because Lua does not recognize
*yas a valid format specifier.Only predefined values such as:
*n *l *a
are accepted.
The programmer cannot invent new ones.
Demonstration
The following program works perfectly:
function fact(y) if y == 0 then return 1 else return fact(y - 1) * y end end userInput = io.read("*n") print(fact(userInput))Notice that:
- The parameter is named
y. - The input variable is named
userInput. - The format specifier remains
*n.
The program still works because the three names serve completely different purposes.
An Analogy
Imagine a teacher taking attendance.
The parameter:
function fact(n)
is like giving a student a name tag.
The name tag can say:
n y number value banana
or almost anything else.
By contrast:
io.read("*n")is like pressing a button on a machine.
The button label is fixed by the manufacturer.
You cannot rename the button.
You must press the button exactly as it was designed.
Key Takeaways
- The
ninfact(n)is a programmer-defined parameter. - The
ninio.read("*n")is a Lua format specifier. - The two uses of
nare completely unrelated. - You may rename the function parameter.
- You may rename the variable receiving input.
- You cannot replace
*nwith*yor another arbitrary value. *nis part of Lua’s built-in syntax for reading numbers.
Final Thought
This confusion occurs because the same letter appears in two different places, even though it represents two entirely different concepts. Understanding the difference between programmer-defined names and language-defined syntax is an important step toward becoming comfortable with any programming language.
- The parameter is named
-
AuthorPosts
- You must be logged in to reply to this topic.
