July 2018
Beginner
202 pages
5h 42m
English
Lua has a unique feature that many traditional languages don't, multiple return values. This feature allows one function to return multiple values. To return multiple values, assign the result of the function to a list of variables separated by commas.
For example, you could write a function that takes a number for an argument and returns both the squared and cubed values of that number:
-- Declare the functionfunction SquareAndCube(x) squared = x * x cubed = x * x * x return squared, cubedend-- Call the functions, c = SquareAndCube(2)print ("Squared: " .. s) -- will print: Squared: 4print ("Cubed: " .. c) -- will print: Cubed: 8
Like with function arguments, the number of values a function returns does not have ...