Functions and Scope
Multiple Returns
Lua functions can return more than one value.
Multiple Returns
multiple_returns.lua
local total =
local function split_total(value)
local left = value / 2
local right = value - left
return left, right
end
local first, second = split_total(total)
print("total=" .. total)
print("first=" .. first)
print("second=" .. second)
local total =
local function split_total(value)
local left = value / 2
local right = value - left
return left, right
end
local first, second = split_total(total)
print("total=" .. total)
print("first=" .. first)
print("second=" .. second)
local total =
local function split_total(value)
local left = value / 2
local right = value - left
return left, right
end
local first, second = split_total(total)
print("total=" .. total)
print("first=" .. first)
print("second=" .. second)
several results
Assign each returned value to its own local name when both results matter.