Med Written By Halalaluyafail3

Variadic Functions Created on: 10-11-2019

Accepting an infinite number of arguments

Including '...' at the end of a parameter list makes the function a Variadic function.

local function a(b,c,...)

end

Whenever you put '...' in a Variadic function, it represents all extra arguments. Wrapping it in parenthesis '(...)' will force it to the first extra argument or nil.

Using 'select'

One of the built in functions, can be used to either get the amount of arguments passed, or get the Xth argument and above (negative number will get the last arguments, aka a negative index)

-- "#" option is used for length
print(select("#",1,2,3,4,nil))
--> 5
print(select(3,1,2,3,4,nil))
--> 3  4  nil
print(select(-2,1,2,3,4,nil))
--> 4  nil

Using the vararg expression

The vararg '...' can be used like any other variable name (expect when indexing/calling)

local function test(...)
	local first,second,third = ... -- can get arguments like this
	print(first,second,third)
	print(...)
	print("amount of args:",select("#",...))
	print("last 3 arguments:",select(-3,...))
	print("excluding first 2 arguments:",select(3,...))
	return select(-1,...) -- return last argument
end
local last = test(1,2,3,4,5,6,7,8,9)
print("last argument:",last)
--> 1 2 3
--> 1 2 3 4 5 6 7 8 9
--> amount of args: 9
--> last 3 arguments: 7 8 9
--> exclusing first 2 arguments: 3 4 5 6 7 8 9
--> last argument: 9

Passing something after a vararg expression

Whenever you pass something after a tuple, the tuple gets forced to a length of 1.

ex:

local function test(...)
	print(select("#",...))
	print(select("#",...,nil))
	print(select("#",(...)))
end
test(1,2,3,4,5)
--> 5
--> 2
--> 1

To get around this, tables must be used.

local function test(...)
    local packed,length = {...},select("#",...)
	local lengthP1 = length+1
	packed[lengthP1] = 6
	return table.unpack(packed,1,lengthP1)
end
print(test(1,2,3,4,5))
--> 1 2 3 4 5 6

Getting the nth argument

Often with a vararg, you will want to get the nth argument. This can be be done with the previously mentioned 'select'.

local function test(arg,...)
	local narg = select(arg,...)
	print(narg)
end
test(2,"a","b","c")
--> b
test(5,5,4,3,2,1)
--> 1
test(-1,5,4,3,2,1)
--> 1

Example

A sum function which will add all arguments passed.

local function sum(...)
	local args = select("#",...)
	local sum = 0
	for i=1,args do
		sum = sum + select(i,...)
		-- in this case the first result of select is used
	end
	return sum
end
print(sum(1,2,3))
--> 6
print(sum(1,-1,0))
--> 0

Med Written By Halalaluyafail3

See Halalaluyafail3's profile on Roblox

Discussion