Lua by Example: Multiple Results

Lua has built-in support for multiple results.

This function returns two number results

local function values()
    return 3, 7
end

Here we use the 2 different return values from the call with multiple assignment.

a, b = values()
print(a)
print(b)

If you only want a subset of the returned values, use the blank identifier _.

_, c = values()
print(c)

The Multiple Results page in the Programming in Lua book has more examples that cover the many edge cases

$ lua multiple-results.lua
3
7
7

Accepting a variable number of arguments is another nice feature of Lua functions; we’ll look at this next.

Next example: Variadic Functions.