function foo()
local x, y = something(4, 5)
return x ^ y
end
Multiple return values
-- Functions can return multiple values!
function something(arg1, arg2)
local ret1 = (arg1 * arg2) ^ 2
local ret2 = (arg1 - arg2) ^ 2
return ret1 + ret2, ret1 * ret2
end
Also returns, function calls, and assignments all work with lists that can be mismatched in length. Any unmatched receivers are nil. Unmatched senders are discarded.
x, y, z = 1, 2, 3, 4
function bar(a, b, c)
print(a, b, c)
return 4, 8, 15, 16, 23, 42
end
x, y = bar('zaphod') --> prints "zaphod nil nil"
-- Now x = 4, y = 8, values 15...42 are discarded.
No parens
Bit silly, but there's a special case for functions which take a single string parameters. They do not need parentheses:
print 'hello world'