I know that declare a function, ...
would act as tuple, for example:
local function hey(...) local t = {...} local Concat = "" for i, v in pairs (t) do Concat = Concat..v end print(Concat) end hey(1, 2, 3, "hey") --output: 123hey
But the thing I don't know that is we can use ...
as the arguments when call a function too, you can see the example in here(go to line 30-32).
Can someone explain for me, thanks :D
It just means it can hold undetermined amount of parameters
example :
function PrintStuff ( . . . ) -- A function with tuple parameter local tupleTable = { . . . } --Putting it in a table variable so we can access it easily print (tupleTable[1]) -- Print the first parameter for _,v in pairs(tupleTable) do print (v) -- Print all the object in the table end end PrintStuff ("Hello", 3, 2, 'A') -- Printing 4 different stuff PrintStuff ("Ok", "screeeee") -- printing 2 stuff PrintStuff (3.1415926) -- printing a single thing
I know this is already answered, but I'd like to add on to what Azure said.
So in the Java programming language, there's a concept called "overloading", it means that you can have 2 functions with the same name but 2 different sets of arguments:
function a(arg1) print(arg1) end function b(arg1,arg2) return arg1+arg2 end
Unfortunately, this will not work in lua, which is where the ...
argument comes in. You can do the same thing as above, just structured differently:
function a(...) local args = {...} if #args == 1 then print(args[1]) elseif #args == 2 then return args[1]+args[2] end end
I just checked for the number of arguments in the table because I knew the first function took 1 argument and the second took 2, but you can also check the type and value of the arguments to set them apart allowing you to have more freedom in adding as much functionality as you want to the function a().