Multiple Return Values

In many languages, a function returns at most one value. In Lua, return can return several, separated by commas. Emma writes down how many minutes her farm work takes, and this function turns minutes into hours and minutes:

local function hoursAndMinutes(minutes)
    return minutes // 60, minutes % 60
end

local hours, rest = hoursAndMinutes(135)
print(hours .. " h " .. rest .. " min")

This prints:

2 h 15 min

The call returns two values, and they go into the variables in order: the first into hours, the second into rest. Lua can assign several variables at once anywhere, not just with functions: local a, b = 1, 2. When there are more variables than values, the extra variables get nil; extra values are thrown away. When you need only the second value, write _ in place of the first, as with ipairs: local _, rest = hoursAndMinutes(135).

print prints all the values a function returns, separated by tabs. But watch what happens when the call isn't last:

local function hoursAndMinutes(minutes)
    return minutes // 60, minutes % 60
end

print(hoursAndMinutes(135))
print(hoursAndMinutes(135), "hours")
print((hoursAndMinutes(135)))

This prints:

2	15
2	hours
2

A call gives all its values only when it's the last (or the only) item in a list of values, such as the arguments of print. Anywhere else, only its first value is used, so the second line prints 2 and hours, and the 15 is lost. Extra parentheses around a call, as on the third line, also keep only the first value.

Several return values are handy when a function finds out more things at once. This function goes through a list once and returns both its smallest and its largest number:

local function smallestAndLargest(list)
    local smallest = list[1]
    local largest = list[1]
    for _, value in ipairs(list) do
        smallest = math.min(smallest, value)
        largest = math.max(largest, value)
    end
    return smallest, largest
end

local low, high = smallestAndLargest({7, 3, 9, 4})
print("From " .. low .. " to " .. high)

This prints:

From 3 to 9

Without several return values, you'd need two functions, and the list would be gone through twice. Both values belong to a single return, separated by a comma. Two returns one after the other don't work: the first one would end the function, and Lua doesn't even allow a line after return.

Instructions

Emma packs eggs into boxes of 6. The program ends with the error attempt to call a nil value (global 'packEggs'), because the function doesn't exist yet. Write it above the loop: packEggs has a parameter eggs and returns two values: how many full boxes of 6 eggs Emma fills, and how many eggs are left over. The program then prints:

Day 1: 3 boxes, 2 left
Day 2: 5 boxes, 1 left
Day 3: 2 boxes, 0 left
Day 4: 3 boxes, 0 left
Day 5: 4 boxes, 1 left

Below the loop, write a function totalAndAverage with a parameter list. It returns two values: the sum of the numbers in the list and their average. Use it for week, and then for the list {6, 9}, and print the results like this:

Total: 106, average: 21.2
Total: 15, average: 7.5

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

4/11