What is “return” and what’s its use in roblox scripting?

Hey guys, so I have been searching for so much time, I have watched so many videos, read some articles, and I still don’t understand the point of the code “return” in a function.

Can you guys please explain the code “return” in easy words? Also can you guys please tell on what it can be useful for, because so far I think it’s useless since you can have all the variables on top!

6 Likes

Here is an example:

local function GetSquare(x)
    return x * x
end

print(GetSquare(5))

-- Prints 5 square = 25

It is used to return a value from a function

Here is other example of it:

local function GetSquareRoot(x)
    if x < 0 then return end -- Stops the function
    return x ^ (1/2)
end

Note that once the return keyword is executed then the code inside that function after the return is wrote wont be executed…

like in the above example if x < 0 then it returns nil and after that the interpreter exits the function… But if x is not less than 0 then it returns the square root.

6 Likes

return stops the function and makes the function called be the value returned, for example

function lol()
return 5
end

hello = lol()

hello is now equal to 5

6 Likes

I have made a post like this:

If you add a sum into a function and return the sum, basically the answer will be the one returned. This is a specific example

function foo()
    local sum = 10 + 10
    return sum
end

print(foo())

This prints 20. The answer.

2 Likes

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 returning. 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.

40 Likes

Wow now that makes a lot more sense. Thanks a lot!

1 Like