9951 explained code solutions for 126 technologies


luaUsing variable arguments


function hi(...)
  local arg={...}
  for i,v in ipairs(arg) do
    print('Hi ' .. v .. '!')
  end
end

hi('Donald')
hi('Joe', 'Donald')ctrl + c
function hi

declare sample function with variable arguments

local arg

make arg variable contain list of function arguments

for i,v in ipairs(arg)

iterate through all arguments

print('Hi ' .. v .. '!')

sample code that prints argument value (access from v variable)


Usage example

function hi (...)
  local arg={...}
  for i,v in ipairs(arg) do
    print('Hi ' .. v .. '!')
  end
end

hi('Donald')
hi('Joe', 'Donald')
output
Hi Donald!
Hi Joe!
Hi Donald!