sugar for
`f('` `string` `')`, that is, the argument list is a single literal string.
As an exception to the free-format syntax of Lua, you cannot put a line break
before the `(` in a function call. This restriction avoids some ambiguities
in the language. If you write
>lua
a = f
(g).x(a)
<
Lua would see that as a single statement, `a = f(g).x(a)`. So, if you want two
statements, you must add a semi-colon between them. If you actually want to
call `f`, you must remove the line break before `(g)`.
*lua-tailcall*
A call of the form `return` `functioncall` is called a tail call. Lua
implements proper tail calls (or proper tail recursion): in a tail call, the
called function reuses the stack entry of the calling function. Therefore,
there is no limit on the number of nested tail calls that a program can
execute. However, a tail call erases any debug information about the calling
function. Note that a tail call only happens with a particular syntax, where
the `return` has one single function call as argument; this syntax makes the
calling function return exactly the returns of the called function. So, none
of the following examples are tail calls:
>lua
return (f(x)) -- results adjusted to 1
return 2 * f(x)
return x, f(x) -- additional results
f(x); return -- results discarded
return x or f(x) -- results adjusted to 1
<
------------------------------------------------------------------------------
2.5.9 Function Definitions *lua-function-define*
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 f ()` `body` `end`
not to
`local f = function f ()` `body` `end`
(This only makes a difference when the body of the function contains
references to `f`.)
*lua-closure*
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 [ , ... ] | ...
<
*lua-vararg*
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