The syntax for function definition is

	function ::= *function* funcbody
	funcbody ::= `*(*` [parlist1] `*)*` block *end*

The following syntactic sugar simplifies function definitions:

	stat ::= *function* funcname funcbody
	stat ::= *local* *function* Name funcbody
	funcname ::= Name {`*.*` Name} [`*:*` Name]

The statement

     function f () _body_ end

translates to

     f = function () _body_ end

The statement

     function t.a.b.c.f () _body_ end

translates to

     t.a.b.c.f = function () _body_ end

The statement

     local function f () _body_ end

translates to

     local f; f = function () _body_ end

not to

     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 pre-compiles a chunk, all its function bodies are pre-compiled 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. Different instances of the same function may refer to different external local variables and may have different environment tables.

Parameters act as local variables that are initialized with the argument values:

	parlist1 ::= 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 variadic or 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 the call is enclosed in parentheses).

As an example, consider the following definitions:

     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:

     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 Control Structures). If control reaches the end of a function without encountering a return statement, then the function returns with no results.

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

     function t.a.b.c:f (_params_) _body_ end

is syntactic sugar for

     t.a.b.c.f = function (self, _params_) _body_ end