3.4.11 – Function Definitions
The syntax for function definition is
1 2 | functiondef ::= function funcbody funcbody ::= ‘(’ [parlist] ‘)’ block end |
The following syntactic sugar simplifies function definitions:
1 2 3 | stat ::= function funcname funcbody stat ::= local function Name funcbody funcname ::= Name {‘.’ Name} [‘:’ Name] |
The statement
1 | function f () body end |
translates to
1 | f = function () body end |
The statement
1 | function t.a.b.c.f () body end |
translates to
1 | t.a.b.c.f = function () body end |
The statement
1 | local function f () body end |
translates to
1 | local f; f = function () body end |
not to
1 | local f = function () body end |
(This only makes a difference when the body of the function contains references to f
.)
A function definition is an executable expression, whose value has type function. When Lua precompiles a chunk, all its function bodies are precompiled too. Then, whenever Lua executes the function definition, the function is instantiated (or closed). This function instance (or closure) is the final value of the expression.
Parameters act as local variables that are initialized with the argument values:
1 | parlist ::= namelist [‘,’ ‘...’] | ‘...’ |
When a function is called, the list of arguments is adjusted to the length of the list of parameters, unless the function is a vararg function, which is indicated by three dots ('...
') at the end of its parameter list. A vararg function does not adjust its argument list; instead, it collects all extra arguments and supplies them to the function through a vararg expression, which is also written as three dots. The value of this expression is a list of all actual extra arguments, similar to a function with multiple results. If a vararg expression is used inside another expression or in the middle of a list of expressions, then its return list is adjusted to one element. If the expression is used as the last element of a list of expressions, then no adjustment is made (unless that last expression is enclosed in parentheses).
As an example, consider the following definitions:
1 2 3 | function f(a, b) end function g(a, b, ...) end function r() return 1,2,3 end |
Then, we have the following mapping from arguments to parameters and to the vararg expression:
1 2 3 4 5 6 7 8 9 10 11 12 | CALL PARAMETERS f(3) a=3, b=nil f(3, 4) a=3, b=4 f(3, 4, 5) a=3, b=4 f(r(), 10) a=1, b=10 f(r()) a=1, b=2 g(3) a=3, b=nil, ... --> (nothing) g(3, 4) a=3, b=4, ... --> (nothing) g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 g(5, r()) a=5, b=1, ... --> 2 3 |
Results are returned using the return statement (see §3.3.4). If control reaches the end of a function without encountering a return statement, then the function returns with no results.
There is a system-dependent limit on the number of values that a function may return. This limit is guaranteed to be larger than 1000.
The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self
. Thus, the statement
1 | function t.a.b.c:f (params) body end |
is syntactic sugar for
1 | t.a.b.c.f = function (self, params) body end |
Please login to continue.