Menu
Coddy logo textTech

Variadic Functions (...)

Part of the Logic & Flow section of Coddy's Lua journey — lesson 11 of 54.

Sometimes you need a function that can work with any number of arguments. Maybe you want to calculate the sum of two numbers, or three, or ten—without writing separate functions for each case. Lua provides a special syntax for this: the three dots ...

When you place ... in a function's parameter list, it becomes a variadic function that accepts any number of arguments:

function sum(...)
    -- Function body
end

Inside the function, you can access these arguments by using ... again. To work with them more easily, you can pack them into a table using curly braces:

function sum(...)
    local args = {...}
    local total = 0
    for i = 1, #args do
        total = total + args[i]
    end
    return total
end

print(sum(5, 10, 15))  -- Output: 30
print(sum(1, 2, 3, 4, 5))  -- Output: 15

The expression {...} creates a list-style table containing all the arguments passed to the function. You can then iterate through this table just like any other, using the length operator # or ipairs().

challenge icon

Challenge

Easy

Write a function calculateAverage that accepts any number of numeric arguments and returns their average.

Use the variadic syntax ... to accept a flexible number of arguments, pack them into a table, calculate their sum, and divide by the count.

Note: The starter code (marked "Don't touch this code") reads numbers from input, converts each one from text to a number using tonumber(), and stores them in a table called numbers. At the end, it calls your function using unpack(numbers), which spreads the table's values as individual arguments — so your function receives them as .... You do not need to modify or understand this code in detail; just focus on writing calculateAverage.

Logic:

  • Pack all arguments into a table using {...}
  • Iterate through the table to calculate the sum of all numbers
  • Divide the sum by the number of arguments to get the average
  • Return the average as a number

Parameters:

  • ... (numbers): Variable number of numeric arguments

Returns: The average of all provided numbers (number)

Cheat sheet

Lua supports variadic functions that can accept any number of arguments using the three dots syntax ...:

function sum(...)
    -- Function body
end

Inside a variadic function, pack the arguments into a table using {...}:

function sum(...)
    local args = {...}
    local total = 0
    for i = 1, #args do
        total = total + args[i]
    end
    return total
end

print(sum(5, 10, 15))  -- Output: 30
print(sum(1, 2, 3, 4, 5))  -- Output: 15

The expression {...} creates a table containing all arguments. You can iterate through it using the length operator # or ipairs().

Try it yourself

-- Don't touch this code --
local numbers = {}
while true do
    local input = io.read()
    if input == nil or input == "" then
        break
    end
    local num = tonumber(input)
    if num then
        table.insert(numbers, num)
    end
end
-------------------------

function calculateAverage(...)
    -- Write code here
end

local average = calculateAverage(unpack(numbers))
print("Average: " .. average)

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow