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
This lesson assumes you have already completed the Loops lesson.
Create a Lua Script with Functions
To create a Lua script with functions:
- Navigate to Module:Sandbox.
- Clear all existing code.
- It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
- Add the following code and save the page:
local p = {} local function toCelsius(f) return (f - 32) * 5 / 9 end local function toFahrenheit(c) return c * 9 / 5 + 32 end function p.functions() local temperature local result result = ';Fahrenheit to Celsius\n' for temperature = 0, 100, 10 do result = result .. ':' .. temperature .. ' °F is ' .. string.format('%.1f', toCelsius(temperature)) .. ' °C\n' end result = result .. ';Celsius to Fahrenheit\n' for temperature = 0, 100, 10 do result = result .. ':' .. temperature .. ' °C is ' .. string.format('%.1f', toFahrenheit(temperature)) .. ' °F\n' end return result end return p
Test Your Lua Script
To test your Lua script:
- 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
- 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
- Celsius to Fahrenheit
- 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
To understand your Lua script toCelsius function:
local function toCelsius(f)declares a local function namedtoCelsiusthat accepts a single parameterf, 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 (f - 32) * 5 / 9converts the Fahrenheit temperature into Celsius and returns the result.endends the function.
To understand your Lua script toFahrenheit function:
local function toFahrenheit(c)declares a local function namedtoFahrenheitthat accepts a single parameterc, 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 c * 9 / 5 + 32converts the Celsius temperature into Fahrenheit and returns the result.endends the function.
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 variablestemperatureandresult. Both arenil.result = ';Fahrenheit to Celsius\n'assigns a string literal value to the variableresult.for temperature = 0, 100, 10 docreates a loop code block that will vary the value of the variabletemperaturefrom0to100by10.toCelsius(temperature)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(temperature)calls thetoFahrenheitfunction and passes in the current value oftemperatureas the temperature to be converted.string.format()calls thestringlibraryformatfunction to format the returned Fahrenheit temperature.'%.1f'indicates that the resulting format (%) should be a single decimal place (.1) floating point (f) value.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 (
functions) - library functions (
string.format).
Conclusion
Congratulations! You've now created, tested, and understood a Lua script with functions. Continue on to the Tables lesson.