- This topic is empty.
-
AuthorPosts
-
June 22, 2026 at 1:44 pm #6996
After successfully running a recursive factorial function in Lua, the next step was to modify the program so that it could accept input from the user rather than always calculating the factorial of a fixed number.
This exercise introduced several important programming concepts including functions, recursion, parameters, variables, user input, and scope.
The Original Program
function fact(n) if n == 0 then return 1 else return fact(n - 1) * n end end print(fact(5))This program always calculates the factorial of 5 and displays:
120
To make the program more interactive, the programmer attempted to allow the user to enter a number.
The Modified Version
function fact(n) if n == 0 then return 1 else return fact(n - 1) * n end end print("Enter number") a = io.read("*n") print(fact(n))Although the factorial function itself remained correct, there was a logical mistake in the final line of the program.
Understanding The Factorial Function
The function:
function fact(n)
creates a reusable block of code named
fact.The value inside the parentheses is called a parameter.
Whenever the function is called, a value is supplied for that parameter.
For example:
fact(5)
causes:
n = 5
inside the function.
Similarly:
fact(10)
causes:
n = 10
inside the function.
The parameter only exists while the function is executing.
The Base Case
Every recursive function requires a stopping condition.
In this program:
if n == 0 then return 1serves as the base case.
Without this condition the function would call itself forever and eventually crash.
When
nbecomes zero, recursion stops and the value 1 is returned.
The Recursive Step
The heart of the algorithm is:
return fact(n - 1) * n
This statement tells Lua to:
- Calculate the factorial of a smaller number.
- Multiply the result by the current value of
n.
For example:
fact(5) = fact(4) * 5 = fact(3) * 4 * 5 = fact(2) * 3 * 4 * 5 = fact(1) * 2 * 3 * 4 * 5 = fact(0) * 1 * 2 * 3 * 4 * 5 = 1 * 1 * 2 * 3 * 4 * 5 = 120
Accepting User Input
The line:
print("Enter number")simply displays a message asking the user for input.
The next line:
a = io.read("*n")waits for the user to type a number.
If the user enters:
7
then:
a = 7
after the input operation completes.
Where The Logic Went Wrong
The programmer attempted to display the factorial using:
print(fact(n))
This is a common beginner mistake.
The issue is that
nonly exists inside the function.Outside the function, Lua has no idea what
nrefers to.The variable created from user input was actually:
a
not:
n
Therefore the correct statement should be:
print(fact(a))
Understanding Variable Scope
This mistake highlights an important programming concept called scope.
Scope determines where a variable can be accessed.
In the following code:
function fact(n)
the variable
nbelongs to the function.Outside the function, the variable does not exist.
By contrast:
a = io.read("*n")creates a variable that exists in the main program.
To pass that value into the function, the programmer must write:
fact(a)
which copies the value of
ainto the parametern.
The Correct Program
function fact(n) if n == 0 then return 1 else return fact(n - 1) * n end end print("Enter number") a = io.read("*n") print(fact(a))
Example Execution
Enter number 6 720
The user enters 6.
The value 6 is stored in
a.The function call:
fact(a)
becomes:
fact(6)
which recursively calculates:
6 × 5 × 4 × 3 × 2 × 1
and returns:
720
Key Lessons Learned
- Functions can accept parameters.
- Recursive functions must have a base case.
- User input can be captured using
io.read(). - Variables only exist within their scope.
- A function parameter is different from a variable created elsewhere in the program.
- Values are passed into functions through function calls.
Final Thought
This exercise demonstrates that programming mistakes are often not syntax errors but logical errors. The program structure was almost completely correct, yet a single incorrect variable name prevented it from working as intended. Understanding scope and data flow is one of the most important skills a new programmer can develop.
-
AuthorPosts
- You must be logged in to reply to this topic.
