Handling variable number of arguments in Lua


Here’s a quick example of how variable number of arguments can be handled in Lua. It has been tested with Lua 5.1 and 5.2.

function SomeFunction(...)
    local list = {...}
    print(string.format("Received %d arguments:", #list))

    -- print all arguments
    print(...)

    -- print all arguments again using a for loop
    for i = 1, #list, 1 do
        print(list[i])
    end
end

SomeFunction("hello", "world")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s