Lua/Functions
Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.[1] Functions are code structures used to encapsulate a series of statements that may be called as needed. This lesson will show you how to use functions in your scripts.
Prerequisites
[edit | edit source]This lesson assumes you have already completed the Loops lesson.
Console to learn Lua
[edit | edit source]- In Module:Sandbox, click
Edit sourcewithout modifying the existing code. - Scroll down until
Debug consoleat the end of the page. - Paste the following function and its call together then validate once by Enter: ↲
local function toCelsius(tempFahrenheit) return (tempFahrenheit - 32) * 5 / 9 end print(toCelsius(32))
The result should be:
0
print(toCelsius(68))
20
Paste together the following three lines then validate once by Enter: ↲
local function toFahrenheit(tempCelsius) return tempCelsius * 9 / 5 + 32 end print(toFahrenheit(0)) print(toFahrenheit(20))
32 68
Create a Lua Script with Functions
[edit | edit source]To create a Lua script with functions:
- Go to the top of Module:Sandbox in the editor.
- Replace the existing code pasting the following Lua code without clicking
Publish changes.
local p = {} -- local numerical functions without `p.` can be called from the Debug console or p.functions local function toCelsius(tempFahrenheit) return (tempFahrenheit - 32) * 5 / 9 end local function toFahrenheit(tempCelsius) return tempCelsius * 9 / 5 + 32 end --[[main function can be called from the Debug console or invoked from another Wiki page]]-- function p.functions() -- table to convert temperatures from Fahrenheit to Celsius and the opposite local tempFah; local result = ';Fahrenheit to Celsius\n:Very cold ' for tempFah = 0, 100, 10 do -- loop from 0°F to 100°F by 10. Convert to °C with one decimal if tempFah > 0 then if tempFah == 50 then result = result .. '\n:' else result = result .. '. ' end end -- ^ belongs to the nested if result = result .. tempFah .. ' °F is ' .. string.format('%.1f', toCelsius(tempFah)) .. ' °C' end -- ^one decimal local tempCel result = result .. ' hot\n;Celsius to Fahrenheit\n:Cold ' for tempCel = 0, 100, 10 do -- loop from 0°C to 100°C by 10. Convert to °F with one decimal if tempCel > 0 then if tempCel == 50 then result = result .. '\n:' else result = result .. '. ' end end result = result .. tempCel .. ' °C is ' .. string.format('%.1f', toFahrenheit(tempCel)) .. ' °F' end return result end return p
In Debug console, validate:
print(p.functions())
;Fahrenheit to Celsius :Very cold 0 °F is -17.8 °C. 10 °F is -12.2 °C. 20 °F is -6.7 °C. 30 °F is -1.1 °C. 40 °F is 4.4 °C :50 °F is 10.0 °C. 60 °F is 15.6 °C. 70 °F is 21.1 °C. 80 °F is 26.7 °C. 90 °F is 32.2 °C. 100 °F is 37.8 °C hot ;Celsius to Fahrenheit :Cold 0 °C is 32.0 °F. 10 °C is 50.0 °F. 20 °C is 68.0 °F. 30 °C is 86.0 °F. 40 °C is 104.0 °F :50 °C is 122.0 °F. 60 °C is 140.0 °F. 70 °C is 158.0 °F. 80 °C is 176.0 °F. 90 °C is 194.0 °F. 100 °C is 212.0 °F
Test Your Lua Script
[edit | edit source]To test your Lua script:
- Save the Module:Sandbox to call it from another Wiki page.
- Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
- Add the following code and save the page:
{{#invoke:Sandbox|functions}}
The result should be:
- Fahrenheit to Celsius
- Very cold 0 °F is -17.8 °C. 10 °F is -12.2 °C. 20 °F is -6.7 °C. 30 °F is -1.1 °C. 40 °F is 4.4 °C
- 50 °F is 10.0 °C. 60 °F is 15.6 °C. 70 °F is 21.1 °C. 80 °F is 26.7 °C. 90 °F is 32.2 °C. 100 °F is 37.8 °C hot
- Celsius to Fahrenheit
- Cold 0 °C is 32.0 °F. 10 °C is 50.0 °F. 20 °C is 68.0 °F. 30 °C is 86.0 °F. 40 °C is 104.0 °F
- 50 °C is 122.0 °F. 60 °C is 140.0 °F. 70 °C is 158.0 °F. 80 °C is 176.0 °F. 90 °C is 194.0 °F. 100 °C is 212.0 °F
Understand Your Lua Script
[edit | edit source]toCelsius
[edit | edit source]To understand your Lua script toCelsius function:
local function toCelsius(tempFahrenheit)declares a local function namedtoCelsiusthat accepts a single parametertempFahrenheit, which is the Fahrenheit temperature to be converted.- Declaring the function as
localprevents it from being called from outside the module.
- Declaring the function as
return (tempFahrenheit - 32) * 5 / 9converts the Fahrenheit temperature into Celsius and returns the result.endends the function.
toFahrenheit
[edit | edit source]To understand your Lua script toFahrenheit function:
local function toFahrenheit(tempCelsius)declares a local function namedtoFahrenheitthat accepts a single parametertempCelsius, which is the Celsius temperature to be converted.- Declaring the function as
localprevents it from being called from outside the module.
- Declaring the function as
return tempCelsius * 9 / 5 + 32converts the Celsius temperature into Fahrenheit and returns the result.endends the function.
p.functions
[edit | edit source]To understand your Lua script functions function:
function p.functions()declares a function namedfunctions.- This function is not declared
local, so it can be called from outside the module.
- This function is not declared
localand the following code defines the variablestempFahandresult.result = ';Fahrenheit to Celsius\n:Very cold 'assigns a string literal value to the variableresult.for tempFah = 0, 100, 10 docreates a loop code block that will vary the value of the variabletempFahfrom0to100by10.toCelsius(tempFah)calls thetoCelsiusfunction and passes in the current value oftemperatureas the temperature to be converted.string.format()calls thestringlibraryformatfunction to format the returned Celsius temperature.'%.1f'indicates that the resulting format'%'should be a single decimal place'.1'floating point'f'value.toFahrenheit(tempCel)calls thetoFahrenheitfunction and passes in the current value oftemperatureas the temperature to be converted.string.format()calls thestringlibraryformatfunction to format the returned Fahrenheit temperature.return resultreturns the current value ofresultas the result of the function.
It should be noted that this script makes use of three different function types:
- locally accessible functions (
toCelsius,toFahrenheit) - globally accessible functions (
p.functions) - library functions (
string.format).
Conclusion
[edit | edit source]Congratulations! You've now created, tested, and understood a Lua script with functions. Continue on to the Tables lesson.
See Also
[edit | edit source]References
[edit | edit source]- ↑ "Extension:Scribunto/Lua reference manual". Mediawiki. 2026. Retrieved 2026-04-06.