function
The function keyword declares a Lua function (functions can also be written in C).
Functions take zero or more arguments (which become local variables inside the function), like this:
Functions can also be declared in an assignment statement like this:
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.
For example:
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:
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:
Functions take zero or more arguments (which become local variables inside the function), like this:
function foo (a, b, c)
return a + b + c
end -- function foo
If you don't require arguments you still need the brackets:
function foo ()
print "hi there"
end -- function foo
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:
foo = function (a, b, c)
return a + b + c
end -- function
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.
For example:
function f (...)
print (select ("#", ...)) --> 4
print (select (2, ...)) --> 20 30 40
end -- function f
f (10, 20, 30, 40)
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:
a = f -- variable a now contains function f
a = f () -- calls function f, puts first result (if any) into a
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:
print
("hello")
However this is OK:
print (
"hello")
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:
table.sort ( { 22, 33, 44, 66, 99 },
function (k1, k2) return k2 < k1 end )
for k, v in ipairs (t) do print (v) end
Output:
99
66
44
33
22
In that example, an anonymous table was sorted using an anonymous comparison function.Lua keyword/topics
- assignment
- break
- comments
- data types
- do
- for (generic)
- for (numeric)
- identifiers
- if / then / else
- keywords
- local
- logical operators
- precedence
- relational operators
- repeat
- return
- string literals
- tables
- while
Topics
- Lua LPEG library
- Lua PCRE regular expression functions
- Lua SQLite (database) interface
- Lua base functions
- Lua bc (big number) functions
- Lua bit manipulation functions
- Lua coroutine functions
- Lua debug functions
- Lua io functions
- Lua math functions
- Lua os functions
- Lua package functions
- Lua script extensions
- Lua string functions
- Lua syntax
- Lua table functions
- Lua utilities
- Scripting callbacks - plugins
- Scripting