Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Why we can use ... as the arguments when calling a function?

Asked by 3 years ago
Edited 3 years ago

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

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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

0
I mean when we call the function not when we declare it. Block_manvn 395 — 3y
1
You simply give the tuple from where the original tuple is from to the function when you call it, i checked the link you gave, it takes whatever parameters inside line 30's tuple, and then give it to line 31, which fired Line 53's function and give whatever parameter inside the tuple to the function, which is the part that triggers line 30's .Touched event. Azure_Kite 885 — 3y
0
I get it now, thank you for answered my question. Block_manvn 395 — 3y
0
Quick thing to add, in lua, tables start with the index at 1 not 0. But don't worry about it too much, it's a common mistake especially if your main language is not lua. SteamG00B 1633 — 3y
0
@SteamG0D yes you are rightt, thanks Azure_Kite 885 — 3y
Ad
Log in to vote
2
Answered by
SteamG00B 1633 Moderation Voter
3 years ago

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().

0
Thank you for explain more deeper to me, it really helped me. Block_manvn 395 — 3y
0
This method is also very useful in module scripts if you know about them. SteamG00B 1633 — 3y

Answer this question