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)

Lua keyword/topics

Topics