Lua Functions

Functions are first-class values in Lua. local function average(values) declares a function visible in its scope, and return sends back a result. ipairs walks the array part of a table in order, giving each index and value; _ names an index we do not need.

Lua
local function average(values)
  local total = 0
  for _, v in ipairs(values) do
    total = total + v
  end
  return total / #values
end

print(math.floor(average({82, 91, 76, 95})))
Read the full lesson