Jump to content




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


  • You cannot reply to this topic
2 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 24 April 2013 - 02:09 AM

Yea, question is in the title ^_^

thx for reading
-Freack100-

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 24 April 2013 - 02: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 by theoriginalbit, 24 April 2013 - 02:17 AM.


#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 24 April 2013 - 02:18 AM

View Posttheoriginalbit, on 24 April 2013 - 02:14 AM, said:

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users