Why use a variadic function instead of passing through a table?

I’m a little confused on why variadic functions were considered useful when passing through a table takes less work to achieve the same results.

Here’s an example:

function passthroughtable(args)
	print(args[1], args[2])
end

function variadic(...)
	local args = table.pack(...)
	print(args[1], args[2])
end
variadic("hello", "goodbye")
passthroughtable({"hello", "goodbye"})

I’m curious to know if variadic functions are better in this case, but i’m also confused with why they are a thing in the first place when there’s already a way to achieve the same thing.

In this scenario both functions do the same thing. But it appears that the function with a table as the parameter is less work than variadic functions.

So what’s the point in using a variadic function if you can achieve the same results but easier by just passing through a table?

You can still add parameters before the ..., so for example:

local function someFunction(width, height, depth, ...)

There aren’t a whole lot of use cases, but the only one I can think of right now is for a server-sided remote event connection, since the player is always the first parameter:

someRemoteEvent.OnServerEvent:Connect(function(plr, ...)
end)

But obviously you can still replace those with a single table, right? Well the only other reason I can think of not just having a table parameter is simply laziness, since there are some cases where you won’t need to table.pack the arguments, so it saves headache(s) to avoid needing the curly brackets when calling the function.

It’s entirely syntactic - and it’s an artefact from C, which is what Lua uses for its implementation.

Because wrapping your args into a table outside of your function declaration is ugly to some people - why do that when you can hide that away?

Some people would say it’s for typechecking, but that’s not entirely true. You can do that just as well with tables as with varargs.

There’s also no real speed difference either - you’re creating and looping through a table regardless if you are passing varargs or a table into your function.

2 Likes

Well the only other reason I can think of not just having a table parameter is simply laziness, since there are some cases where you won’t need to table.pack the arguments, so it saves headache(s) to avoid needing the curly brackets when calling the function.

Good point, I think it’s more of just a headache to constantly call a function with a table constructor.

You can also just run a function call with a table constructor which is even more simple and less work. Another example of this;

function pie(args)
print(args[1], args[2])
end
pie{1,2}

But yeah it’s the same issue because you’re always having to use the curly brackets to achieve the parameter being a singular table. Why do that when you can just make a variadic function with lua ... syntax

It’s entirely syntactic - and it’s an artefact from C, which is what Lua uses for its implementation.

Also with what @Solar_Eos said I guess that makes sense if it’s originally from C

All in all I think everyone’s points may be valid in this case, and I think certain methods may be more relative to what you’re trying to do. Appreciate everyone’s opinion on this.

1 Like