Garry's Mod Wiki

Revision Difference

vararg#561743

<title>Concepts - Variable Number of Arguments (var args)</title>⤶ <cat>Dev.Lua</cat> ⤶ # Variable Number of Arguments (var args)⤶ Varargs are variable number of arguments, meaning the amount of arguments is undefined.⤶ The most notable function to take varargs is <page>Global.print</page>. ⤶ To create a function with varargs, you must set the **last** parameter of the function to **...** (that's three⤶ periods). You may use a combination of normal arguments with varargs, but the varargs **must** appear as the last⤶ parameter. Inside of the function's body, you may access each argument by creating a table of the varargs with **{ ... }**.⤶ ⤶ In the table, each key is the respective number of the argument, with the value being the value passed. ⤶ Varargs are stored at negative indices, so functions such as <page>debug.getlocal</page> may access the values where⤶ `-1` is the first vararg passed, `-2` is the second, etc.⤶ ⤶ You may create varargs yourself with <page>Global.unpack</page>.⤶ ⤶ <example>⤶ <description>⤶ Finds the average of a set of numbers.⤶ </description>⤶ ⤶ <code>⤶ function average( ... )⤶ local args = { ... }⤶ local result = 0⤶ ⤶ for _, v in ipairs( args ) do⤶ result = result + v⤶ end⤶ ⤶ return result / #args⤶ <title>Concepts - Variadic Functions</title>⤶ ⤶ # Variadic Functions⤶ ⤶ Functions that take a variable amount of arguments are called `variadic`. ⤶ <br/>⤶ ⤶ ## Example⤶ ⤶ The `print` function logs any number of values you supply it to the console. ⤶ ```Lua⤶ print( 'These' , 'are' , 'a' , 'lot' , 'of' , 'arguments' , 1 , 2 , 3 ) ``` ⤶ ⤶ <br/>⤶ ⤶ ## Declaration⤶ ⤶ Variadic arguments can be declared with the name ⤶ `...` at the ends of a function's list of arguments.⤶ ⤶ ```Lua⤶ function ( first , second , ... )⤶ ```⤶ ⤶ Calling the function with `( 1 , 2 , 3 , 4 )` results in:⤶ ⤶ - `first` = `1`⤶ - `second` = `2`⤶ - `...` = `3 , 4`⤶ ⤶ <br/>⤶ ⤶ ## Usage⤶ ⤶ You can use supply `...` as an argument to another function.⤶ ⤶ ```Lua⤶ sum( 1 , 2 , ... , 5 ) -- Same as sum( 1 , 2 , 3 , 4 , 5 )⤶ ```⤶ ⤶ <br/>⤶ ⤶ ## Unpacking⤶ ⤶ You can also create variadic arguments by <page text = 'unpacking' >Global.unpack</page> a table.⤶ ⤶ ```Lua⤶ unpack({ 3 , 4 }) -- Same as `3 , 4`⤶ ```⤶ ⤶ <br/>⤶ ⤶ ## Packing⤶ ⤶ The opposite can be achieved by <page text = 'packing' >table.Pack</page> a table.⤶ ⤶ ```Lua⤶ local arguments = table.Pack( ... ) -- Same as { 3 , 4 }⤶ ```⤶ ⤶ ```Lua⤶ local arguments = { ... } -- Shorthand Syntax⤶ ```⤶ ⤶ <br/>⤶ ⤶ ---⤶ ⤶ <br/>⤶ ⤶ ⤶ <example name = 'Summation' >⤶ ⤶ <description>⤶ ⤶ Adds up a variable numbers of values.⤶ ⤶ </description>⤶ ⤶ <code>⤶ ⤶ function sum ( ... )⤶ ⤶ local total = 0⤶ ⤶ for _ , value in ipairs({ ... }) do⤶ total = total + value⤶ end⤶ ⤶ return total⤶ end⤶ ⤶ </code>⤶ ⤶ <code name = 'Test' >⤶ ⤶ print(sum(1,2,3))⤶ ⤶ </code>⤶ ⤶ <output>6</output>⤶ ⤶ </example>⤶ ⤶ <br/>⤶ ⤶ ---⤶ ⤶ <br/>⤶ ⤶ <example name = 'Average' >⤶ ⤶ <description>⤶ ⤶ Finds the mean of a variable numbers of values ⤶ using the previous example's summation function.⤶ ⤶ </description>⤶ ⤶ <code name = 'Function' >⤶ ⤶ function average ( ... )⤶ return sum( ... ) / #{ ... }⤶ end ⤶ print( average( 5, 15, 30, 7, 52 ) ) </code>⤶ <output>21.8</output>⤶ </code>⤶ ⤶ <code name = 'Test' >⤶ ⤶ print(average(1,2,3)) ⤶ </code>⤶ ⤶ <output>2.0</output>⤶ </example>