This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

How can I make a function without defined count of arguments?

Started by H4X0RZ, 24 April 2013 - 01:09 AM
H4X0RZ #1
Posted 24 April 2013 - 03:09 AM
Yea, question is in the title ^_^/>

thx for reading
-Freack100-
theoriginalbit #2
Posted 24 April 2013 - 03:14 AM
Like so:

function someName( ... )
  local params = { ... }
  for _,v in ipairs(params) do
	print(v)
  end
end

someName( 'hello' )
someName( 'hello', ' ', 'world', '!' )

EDIT: more examples
if you didn't do this

local params = ...
you would only get the first one, hence putting it in a table
you can even do this

local p, a = ...
and it will get the first 2
lastly you can also do this

function someName( something, ... )
and the first value passed in will go into something the rest goes into the …
however you cannot do this

function someName( ..., something )
Edited on 24 April 2013 - 01:17 AM
H4X0RZ #3
Posted 24 April 2013 - 03:18 AM
Like so:

function someName( ... )
  local params = { ... }
  for _,v in ipairs(params) do
	print(v)
  end
end

someName( 'hello' )
someName( 'hello', ' ', 'world', '!' )

EDIT: more examples
if you didn't do this

local params = ...
you would only get the first one, hence putting it in a table
you can even do this

local p, a = ...
and it will get the first 2
lastly you can also do this

function someName( something, ... )
and the first value passed in will go into something the rest goes into the …
however you cannot do this

function someName( ..., something )
Thanks a lot!