Before you understand return
you need to understand a function’s purpose.
What is a function’s purpose
The purpose of a function is to shorten and simplify your code. For instance say I have this code that changes the color of 3 parts to green then to blue.
local parts = { workspace.Part1, workspace.Part2, workspace.Part3 }
for _, part in ipairs(parts) do
parts[1].BrickColor = BrickColor.new("Bright blue")
parts[2].BrickColor = BrickColor.new("Bright blue")
parts[3].BrickColor = BrickColor.new("Bright blue")
wait(1)
parts[1].BrickColor = BrickColor.new("Bright green")
parts[2].BrickColor = BrickColor.new("Bright green")
parts[3].BrickColor = BrickColor.new("Bright green")
wait(1)
end
for _, part in ipairs(parts) do
parts[1].BrickColor = BrickColor.new("Bright blue")
parts[2].BrickColor = BrickColor.new("Bright blue")
parts[3].BrickColor = BrickColor.new("Bright blue")
wait(1)
parts[1].BrickColor = BrickColor.new("Bright green")
parts[2].BrickColor = BrickColor.new("Bright green")
parts[3].BrickColor = BrickColor.new("Bright green")
wait(1)
end
for _, part in ipairs(parts) do
parts[1].BrickColor = BrickColor.new("Bright blue")
parts[2].BrickColor = BrickColor.new("Bright blue")
parts[3].BrickColor = BrickColor.new("Bright blue")
wait(1)
parts[1].BrickColor = BrickColor.new("Bright green")
parts[2].BrickColor = BrickColor.new("Bright green")
parts[3].BrickColor = BrickColor.new("Bright green")
wait(1)
end
Look at all that unnecessary copy-pasting! Using a function we can shorten (as well as simplify) this script.
local function edit_color(brick_color)
for _, part in ipairs(parts) do
part.BrickColor = brick_color
end
end
edit_color(BrickColor.new("Bright green"))
wait(1)
edit_color(BrickColor.new("Bright blue"))
The second one is much easier to read as well as edit.
An analogy
Just like how a variable is a container of arbitrary data, you can think of a function that is a “variable that holds code” that you can reuse when necessary.
local function greet(name)
print("Hello,", name .. "!")
end
greet("incapaz")
-- Same as VVV
print("Hello, incapaz!")
local g = 5
print(g)
-- ^ same as vvv
print(5)
I hope you understand this because from here we will see how return
can fit this analogy.
What does return
do
Returning gives something back to you from a function call.
local function is_even(n)
if n%2 == 0 then
print("true")
else
print("false")
end
end
is_even(5)
is_even(16)
Output is
false
true
Now suppose you have a few parts in your game. You want them to have collisions based on a given number’s parity. You could do workspace.Map.Door.CanCollide = blahblahblah
but what about other parts? Like a wall? You could add another argument but why unnecessarily complicate this? return
can be used to make your life easier.
local function is_even(n)
return n%2 == 0
end
workspace.Map.Door.CanCollide = is_even(5)
workspace.Map.HiddenWall.CanCollide = is_even(6)
In this example I am calling the is_even()
method, and giving it an argument. Since the function returns a boolean that determines whether or not the given number is even, the CanCollide
properties are set to true or false depending on the number’s parity.
So, the expression is_even(n)
is a boolean. That is the magic in return
ing. The function call evaluates to its return value.
This example shows you better:
local function two_hundred()
return 200
end
print(1000 - two_hundred()) -- 800
The function two_hundred
points to is called, and its result is used as the right-hand side for the -
operator.
More on return
-
You can use it as a guard clause. If some certain condition is met that you don’t want met you can prematurely end your function right there.
You have likely seen this example before:
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, engine_processed)
if engine_processed then
return
end
print(input.UserInputType.Name)
end)
In this example if the engine processed our input (because of clicking a button or interacting with a TextBox), we would return
out of the function because the function should not be called in this case. So the function is ended prematurely.
-
You can’t have code after a return
statement.
local function foo()
return
local x = 5
print(x + 50)
end
The code after the return
statement is unreachable, and you get a syntax error.
-
You use functions that return values all the time. You have been generating pseudo-random numbers using the math.random()
method. You have been getting tables of children using the Instance:GetChildren()
method. You have been getting matches of strings using the string.match()
method. You get these results back because of return
.