Programming Fundamentals/Functions/Lua
Appearance
functions.lua
[edit | edit source]-- This program asks the user for a Fahrenheit temperature,
-- converts the given temperature to Celsius,
-- and displays the results.
--
-- References:
-- https://www.mathsisfun.com/temperature-conversion.html
-- https://www.lua.org/manual/5.1/manual.html
function get_fahrenheit()
local fahrenheit
print("Enter Fahrenheit temperature:")
fahrenheit = tonumber(io.read())
return fahrenheit
end
function calculate_celsius(fahrenheit)
local celsius
celsius = (fahrenheit - 32) * 5 / 9
return celsius
end
function display_result(fahrenheit, celsius)
print(fahrenheit .. "° Fahrenheit is " .. celsius .. "° Celsius")
end
function main()
local fahrenheit
local celsius
fahrenheit = get_fahrenheit()
celsius = calculate_celsius(fahrenheit)
display_result(fahrenheit, celsius)
end
main()
Try It
[edit | edit source]Copy and paste the code above into one of the following free online development environments or use your own Lua compiler / interpreter / IDE.