Ad
Log in to vote
0

How would I create a function with an unknown number of arguments?

Asked by
nate890 495 Moderation Voter
7 years ago

[This is a mock question]

Sometimes when I code a function I don't know how many arguments are being inputted because the amount of arguments needed for the function are unknown and can be infinite. This causes a lot of problems in my function and forces me to do something like this,

function printStrings(a, b, c, d, e)
    local strings = {a, b, c, d, e}
    print(table.concat(strings, " "))
end

printStrings("Hello,", "World", "I", "love", "you")

As you can see, that's time consuming and has a limit of 5 arguments. If I wanted to have an infinite number of parameters for my printStrings function, how would I go about doing this?

3 answers

Log in to vote
4
Answered by
MrNicNac 855 Moderation Voter
7 years ago

You're looking for a solution to handling a variable number of arguments. Lua uses three dots (...) to signify an unfixed amount of function parameters. Source

There is not an official name for these dots in Lua, as shown in the source.

This can be applied like so:

function Output(...)
    -- to handle all possibilities, you can dump them into a table
    local arguments = ({...})
    print( table.concat(arguments, "") )
end

Output(1,2,3,true, "String")

Other information

A real example of a function that uses this would be Lua's print function. It can be supplied with a variable number of arguments.

You can also pass all the values from our Output function example to another function by using the unpack function.

AnotherFunction( unpack(arguments) )

It is also worth noting that you can easily define expected parameters prior to using the ... syntax. For example, you might need to expect a bool before outputting the latterly-supplied parameters.

function X(bool, ...)
    if bool then
        print( unpack( ({...}) ) )
    else
        return false
    end
end
0
Great post. This is by far my favorite answer because of the depth and specificy of the content. I also like that you used an example that is meaningful and provided extra content. Kudos.  (The other answers where good too and gave correct content, +1 to all) nate890 495 — 7y
Ad
Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
7 years ago

What you want to write is a variadic function, which supports a variable amount of parameters.

A variadic function in Lua requires the tuple ... to represent the rest of the parameters. You can stuff this into a table and call each argument individually like { ... }. Here's an example...

function printArguments(...)
    local arguments = { ... }
    for key, value in pairs(arguments) do
        print(value)
    end
end
Log in to vote
1
Answered by
Merely 2117 Moderation Voter Community Moderator
7 years ago

Here's the Lua documentation on using a variable number of arguments.

And here's a quick example of how you would use them:

function variableArguments(...)
    local args = {...} --put the arguments in a table
    for i,v in pairs(args) do
        print(i,v)
    end
end

variableArguments("noob", "yolo")

Output: 1 noob 2 yolo

Answer this question