Concepts - Variable Argument Count (varargs)
Variadic Functions
Functions that take a variable amount of arguments are called variadic
.
Example
The print
function logs any number of values you supply it to the console.
Declaration
Variadic arguments can be declared with the name
...
at the ends of a function's list of arguments.
Calling the function with ( 1 , 2 , 3 , 4 )
results in:
first
=1
second
=2
...
=3 , 4
Usage
You can use supply ...
as an argument to another function.
Unpacking
You can also create variadic arguments by unpacking a table.
Packing
The opposite can be achieved by packing a table.
Example: Summation
Adds up a variable numbers of values.
Code
Test
Output: 6