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

[LUA] Is there a way to define a function that can take an unknown number of arguments?

Started by chriskevini, 10 August 2012 - 05:28 PM
chriskevini #1
Posted 10 August 2012 - 07:28 PM
Is there a way to define a function that can take an unknown number of arguments?
MysticT #2
Posted 10 August 2012 - 07:30 PM
Yes. To do it, you put … as parameter, and it will contain all the parameters passed.
Example:

function myFunction(...)
  local tArgs = { ... } -- store the arguments on a table, so we can access them
  for i, v in ipairs(tArgs) do -- for each argument passed
    print(i, ": ", v) -- print the arg number and the value
  end
end
chriskevini #3
Posted 11 August 2012 - 12:31 AM
Thank you, you're awesome!
FuzzyPurp #4
Posted 30 August 2012 - 05:37 AM
Wrong section. Moved to "Ask a Pro"