参数数组

函数

-- The array of values is one parameter
local function get_avg(values)
  if #values == 0 then
    return 0
  end
  local total = 0
  for _, value in ipairs(values) do
    total = total + value
  end
  return total / #values
end

local avg = get_avg({1, 2, 3, 4})
-- avg = 2.5

-- unpack turns an array into separate arguments
local function volume(width, height, depth)
  return width * height * depth
end

local sides = {2, 3, 4}
local space = volume(table.unpack(sides))
-- space = 24

print("avg = " .. avg)
print("space = " .. space)