Register forum user name Search FAQ

return

The return statement exits the current function, optionally returning values to the caller. 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)


See Also ...

Lua keywords/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 base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua SQLite (database) interface
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting
Scripting callbacks - plugins

(Help topic: lua=return)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.