In Lua the statements return
, break
and continue
are all used for jumping out of a block of code. While they may seem very different at first, their behaviour and usage is similar to each other.
The return
statement is used to hand back results from a function, or to simply exit it. By default when a function reaches its end it will return out of itself without giving any value. The return
statement can be used to do this earlier on inside a function before its proper end.
An example of a simple return is as follows:
local function MyFunction(value)
if value == true then
print("Value is true!")
return
end
print("Value is not true!")
end
MyFunction(true)
MyFunction()
Which will print the following:
Value is true! Value is not true!
When we call the function giving it the value of true
, our code will print "Value is true!". As we are telling the function to return after this, the second print
statement is not executed.
On our second call of the function, where no value is given, we see our second print
statement running as expected, as there were no return call to halt it.
As said earlier, return
can also hand back results from a function. This can be used for example to solve math. Instead of having many lines for a math equation that is repeated multiple times, simply have a function that you call upon when you need to!
-- Please imagine a more difficult math equation for a more realistic usage!
local function Multiply(x, y)
return x * y
end
local myNumber = Multiply(3, 7)
print(myNumber)
Which would give us this result:
21
Here we create our value myNumber
and assign it to be what our multiplication function returns to us. This return could be anything you want it to be.
Another interesting usage of return
is that if it is called outside of a function and directly for our script itself, the script will stop then and there, and any code under it will not run just as if inside of a function. For normal scripts this will do nothing, but for Module Scripts this is how we give whatever script that required it what it wants.
The break
statement is very similar to return
, with that it exits out of a block of code. Only in this case, it is used for loops! The break
statement is unable to return any values with it, but it is effective for stopping a loop early on.
for i = 0, 10 do
if i == 5 then
break
end
print(i)
end
Our code here will print out the following:
0 1 2 3 4 Once our loop reaches 5, we tell it to stop and exit itself using our
break
statement. Because of this it will never get the chance to print out the numbers 5 to 10.
The continue
statement is for loops only, just like break
. The difference here is that continue
will only skip the current iteration of the loop. Meaning the loop will skip to the end, and keep going from the top as if it fully completed what it was doing.
for i = 0, 10 do
if i == 5 then
continue
end
print(i)
end
This code will print the following:
0 1 2 3 4 6 7 8 9 10 Notice how our code never printed out 5. This is because we skipped it by using the
continue
statement.
As we now have gone over, the statements return
, break
and continue
are similar to each other, but are still widely different in terms of usage.