How do you use ... as parameters in functions?

Hello there! So, I’ve been seeing that on some modules, and other places, I’ve seen people only putting … as the parameter. Like:

function stuff(...)
    -- Rest of the code
end

I’ve no idea what this is for. It would be great if anybody could explain it to me!

Thanks!

  • Arid

It’s for doing something like that there an small example :slight_smile:

local m = {}

function m.example(...)
	local Args = {...}
	
	print(Args[1]..Args[2]..Args[3])--.. etc
end

m.example("Example1", "Cool", "Stuff")

return m

Should Print > Example1 Cool Stuff

1 Like

Is the … like a table? (pls char limit)

In programming terms it is a variadic function, it allows you to input as many arguments as you want without having to name them all,

To give an example, print is a variadic function because you give it as many inputs as you want

print(10, true, "false", 6475, 6+7)

And it still works

The uses for this can vary, another example, would be a function that returns the sum of given numbers, something like this

function sum(...)
    local sum = 0
    for _, value in ipairs({...}) do
        sum += value
    end
    return sum
end

... would return a tuple of arguments, not a table, so you need to make it a table yourself by encasing it in squiggly brackets in most cases.

This has more info about it which I’d recommend reaidng, it’s also where I took the 2nd example from

2 Likes

So, if I’m correct, then the … is a table that I can use, so I don’t have to name all my parameters? Is that what you meant?

No, ... is a tuple, a tuple is as if you returned more than 1 thing

return 10, 15

It’s not a table, you have to make it a table yourself by putting it around {}.

... is generally used for when you expect yourself/the user to pass in multiple arguments, instead of making a lot of parameters you could use ...

Take the sum example again, we don’t know how many arguments we’ll pass in, they could pass in 2 or 20 for example, so ... is needed there instead of named parameters

2 Likes

Thanks for the help! I’m still a little unclear about it. Could you give me a better example without the printing? Then it might help!

1 Like

That’s all I had to say about it, it’s to indicate that the user can input as many arguments as they want without naming them. The usage depends so I can’t comment on that. I would recommend reading the article I’ve listed in my post if you want more info or to do research.

What exactly are you still unclear about?

1 Like

Actually I understood the basics of it. But I’m looking for another example rather than printing. But if you find any examples, kindly please share it.

By that time I’m just going to play around with it!

I did give another usage of it though, the sum function

function sum(...)
    local sum = 0
    for _, value in ipairs({...}) do
        sum += value
    end
    return sum
end

We first initalize a sum variable, then we turn the ... tuple into a table and loop through it, and we increment sum by the number in that index and then at the end, return the sum of all numbers. So if I did

print(sum(7, 5, 4))

It would print out 16 because the sum of 7, 5 and 4 is 16

2 Likes

Okay, thanks for the help, @EmbatTheHybrid and @iiNeZoXii!

2 Likes