Lesson 21 of 55
9 mins readPython Variadic Functions & Variable Arguments (...)
In Plain English
Functions accepting variable arguments define `...` (three dots) in their parameter list. Access arguments using `table.pack(...)`, `select()`, or by collecting into a table `{ ... }`.
Deep Dive: How It Works
Ellipsis Parameter (...): Represents all passed extra arguments.
Table Packing: `local args = { ... }` or `local packed = table.pack(...)` (stores count in `packed.n`).
select(n, ...): Returns arguments starting from index `n`. `select("#", ...)` returns the total argument count.
Core Rules to Remember

Ellipsis (...) Notation: Captures arbitrary argument counts cleanly.

select("#", ...): Efficiently counts variable arguments without creating table allocations.
Live Interactive Example
Hit Run Code to see it liveSumming Arbitrary Numbers with Varargs
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: { ... } collected the variadic arguments into a table for iteration.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Count Varargs with select
Define `local function count_args(...) return select("#", ...) end`.
Print `"Args count: " .. count_args("a", "b", "c", "d")`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.