The function keyword declares a Lua function (functions can also be written in C).
If you don't require arguments you still need the brackets:
If the caller does not supply all the argument values, the extra ones are set to nil.
Functions can also be declared in an assignment statement like this:
This is equivalent to the earlier example.
Functions can return multiple results (see the return statement for more details).
A special form of function argument is the "..." syntax which represents "a variable number of arguments". If used, it must be the last (or only) argument in the function argument list.
This is useful in cases where you want to pass an unknown number of arguments to a function. The select function is useful for extracting out the individual arguments. Also the unpack function can be used to turn the "..." argument into a table.
Functions are called by supplying the function name followed by the arguments (if any) in brackets. Even if there are no arguments you must use the brackets, otherwise you are just assigning the function. For example:
There is an ambiguity in Lua because of the above, so Lua requires the opening bracket to be on the same line as the function. In other words, this is not permitted:
However this is OK:
Functions can be anonymous, that is, just used where a function is required without assigning them to any variable (and thus they don't have a name). For example, to sort some numbers into descending order:
In that example, an anonymous table was sorted using an anonymous comparison function.
See Also ...
Lua base functions
(Help topic: lua=function)
Enter a search string to find matching documentation.
Gammon Software support
Forum RSS feed ( https://gammon.com.au/rss/forum.xml )