return
The return statement exits the current function, optionally returning values to the caller. For example:
Functions can return multiple values, which can be "captured" by a multiple assignment statement. For example:
return -- exit returning no values
return 1, 2, 3, 4 -- exit returning four values
Every function has an implicit return at the end of it, so you don't normally need to put your own, unless you want to return values.Functions can return multiple values, which can be "captured" by a multiple assignment statement. For example:
function foo ()
return 1, 2, 3, 4
end -- function foo
a, b, c, d = foo () --> assigns the values 1, 2, 3, 4
If a function returns a function call, then multiple values returned by the calling function are returned. For example:
function foo ()
return 1, 2, 3, 4
end -- function foo
function bar ()
return foo ()
end -- function bar
print (bar ()) --> 1, 2, 3, 4
If a function returns multiple values and those values are used in an expression list, then the multiple values are adjusted to one value (the first) unless they are used in the last or only place. For example:
function foo ()
return 1, 2, 3, 4
end -- function foo
print (foo (), 8) --> 1, 8 (other values discarded)
However:
function foo ()
return 1, 2, 3, 4
end -- function foo
print (8, foo ()) --> 8, 1, 2, 3, 4 (all values retained)
Lua keyword/topics
- assignment
- break
- comments
- data types
- do
- for (generic)
- for (numeric)
- function
- identifiers
- if / then / else
- keywords
- local
- logical operators
- precedence
- relational operators
- repeat
- 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