Like @Ragnar_Svensson writes there is no mechanism in the language itself to indicate that a function parameter is optional, but there are plenty of examples in the Lua standard API where optional parameters exist and where the behaviour of a function depends on the number of arguments provided:
table.concat (table [, sep [, i [, j]]])
table.insert (table, [pos,] value)
os.date ([format [, time]])
…and many more.
In your code you can assign default values using the or logical operator like in Ragnar’s example. A lot of Lua libraries use LDoc to write and automatically generate documentation, and optional arguments are documented like this:
--- Three dashes indicate the beginning of a function or field documented
-- using the LDoc format
-- @param var1 The first argument
-- @param[opt=6] var2 The second argument
-- @param[opt=0] var3 The third argument
-- @return Max The maximum value of var1 and var2
-- @return Min The minimum value of var2 and var3
function foobar(var1, var2, var3)
var2 = var2 or 6
var3 = var3 or 0
return math.max(var1, var2), math.min(var2, var3)
end
And the resulting generated docs would look like:
The above example is just a subset of what can be done using LDoc.
