Lua functions can accept a variable number of parameters. Similar to the C language, three dots (...) are used in the function parameter list to indicate that the function has variable parameters. Lua puts the parameters of the function in a table called arg.
In addition to the parameters, there is a field n in the arg table that represents the number of parameters.
For example, we could rewrite the print function:
printResult = ""
function print(...)
for i,v in ipairs(arg) do
printResult = printResult .. tostring(v) .. "\t"
end
printResult = printResult . . "\n"
end
In addition to the parameters, there is a field n in the arg table that represents the number of parameters.
For example, we could rewrite the print function:
printResult = ""
function print(...)
for i,v in ipairs(arg) do
printResult = printResult .. tostring(v) .. "\t"
end
printResult = printResult . . "\n"
end