What does "return" do in a function? I'm clueless

I’ve been trying to learn Roblox scripting for 3 months now. I have a basic understanding of variables, properties, certain loops, if/then statements, functions, and parameters (kind of) but no matter how many videos I watch, how many people I ask in developer hub experiences, how many DevForum posts I view, I still don’t understand how to use it, let alone what it even does. Could anyone please try to break things down and explain it to me?

(If your reply isn’t marked as a solution, that means that either I don’t understand what you’re saying, or there is already a comment marked as a solution)

1 Like

The script gets start over with the value changed. This is useful if you’re trying to load and define things.

local function example(number)
    local multipliedByTwo = number * 2

    return multipliedByTwo
end

local variable = example(10) -- example(10) will turn into 20

print(variable) --> 20

Basically what the value will be after calling the function.

3 Likes

return is self explanatory, it returns something, however it can also be used to end a function early as using return end the function by returning a piece of data

1 Like

Return is a tricky one, but also an important one. Let’s say you do some math in the function, like 2 + 2. Now, how do we even access the value of 2 + 2, well we can’t right now because of scope sadly. But that’s where return comes in this case. Thats when we return 2 + 2. We can now access our answer to this equation. When you call the function, the function ITSELF will become the value, this is what I mean:

function returntest()
    return 2+2
end

local number = returntest() -- returntest literally is just 4 now because we returned 2 + 2, but it stills activates the code inside the function
6 Likes

“return” just basically lets you easily grab variables or stop a function in the middle of a loop

local function Add(X, Y)
   return X + Y -- Returns the two given numbers added together
end

local Number = Add(2, 5) -- 7
-- You can also do print(Add(2,5)) as it returns the same number

-- On top of this, you can also break a loop with a return pretty easily

local function FindDescendantOfName(Path, Name) -- There are better ways to implement this, but this is just a demonstration so it doesn't matter
    for i,v in next, Path:GetDescendants() do
        if v.Name == Name then
            return v -- Only grabs one of the descendants of the given path
        end
    end

    return nil -- If a descendant wasn't found with the name, this basically just sets the default output to nil. Functions return nil by default, but I feel like it's worth mentioning.
end

print(FindDescendantOfName(workspace, "Camera"), "Is the current camera")
print(FindDescendantOfName(workspace, "erajoghbeaiyhg"), "Is Nil")
1 Like

I don’t understand, I haven’t learned about “find descendant” yet not have I learned about i v pairs

Its not that I hate people who use ChatGPT,
what I hate is that people are using it to farm solutions on the DevForum without even knowing or fact checking what the code or wording is.

Edit: This Post existed at a time when there was an Argument about someone using ChatGPT on this topic, and plenty of others. All posts were removed except this one, so now this post seems out of place., and just a random off-topic rant.

9 Likes

FindDescendantOfName is a custom function defined by you, not a roblox function.

If you haven’t learned what pairs is, you should probably learn about that before you learn about returning values in functions. It’s an extremely important feature.

ChatGPT is based off of more professional wording so you can tell which posts are made with it and which are not, there’s online tools to detect AI-Written text too.

5 Likes
local function example()
    return true --When called, it sends this value to the place where it was called.
end

local variable = example() --This function was called and since it returns `true`, variable is true

price(variable) --Prints `true`

If you still do not understand then I cannot help you, sorry.

Darnit, I’m still completely clueless

I have a friend on social media who sent me a script example of how the return function could be used. I experimented with it by removing the line with “return” in it but I still got the same exact output I would’ve got if I kept it in there.


^ With the return function


^ Without the return function

Could anyone explain this to me, and maybe this will help me bring the pieces together?

You’re still printing y and x in the function, you’ve never printed the number variable.

1 Like

Example

So the local function addTwoNumbers has parameters of x, y

Within that function I make a new variable called add. In that variable I add x + y together.
And then I return add back to the function for it to store the addition which has yet been given numbers.

And then I print out the function and then assign numbers 5, 5… which then in turn since the function has been returned with an addition… adds the two numbers together. Does that make sense?

And the same can be said if you make add instead of

add = x + y 

You could as well make it

add = x - y

which then in return make the output 0.

I don’t know guys
When you all say “it returns it back to the function” it sounds super simple but for some reason it just comes across as vague to me.
I’m gonna try and learn other Luau things that I don’t understand yet and maybe some day It’ll all click.
Thank you all for trying to help me and have a good night :+1:

Before you go, let me try.

Here’s a function that adds two numbers a and b. Pretend there’s code inside, and let’s assume both a and b will always be numbers (no strict typing needed here, although it’s good practice).

function add(a, b)
  ...
end

To call the function, we’d simply use add(a, b): for example, add(2, 5).

We want this function to add two numbers. Simple enough – let’s add the two numbers we pass into the function and store that value.

function add(a, b)
  local result = a + b
end

Great. If we call add(2, 5), we’ll get 7. But how do we get this value out of the function?
That’s where the return keyword comes in.

Let’s say we wanted to store that result value outside of the function. To do this, we can use the return keyword:

function add(a, b)
  -- We add the two numbers
  local result = a + b
  -- We return the variable result, which is the sum of a and b
  return result
end

Now, whenever add(a, b) is called, we can use that result value.

Now, we can assign a variable to add(a, b). We can do this because the return value will give us back whatever result is in that function.
local c = add(2, 5)

And as expected, c will have a value of 7.

local c = add(2, 5)
print(c) 
-- 7

Returns are quite simple. Mess around with it in Studio and you’ll get a feel for it.

2 Likes

You know how a function like placeholder_part:IsA("Part") will return true if the instance is a part, and false if not?

return works by giving out a result when a function is called:

local function Chat(part,message)
    --This example is just a shortened version of using game.Chat:Chat()
    local chat = game:GetService("Chat")
    return chat:Chat(part,message)
end

Chat(workspace.Part,"hello") --Part will say "hello" with bubble chat

It’s also commonly used to stop a function from further executing, for example, if a conditional is not met.

local cooldown = false
...
if cooldown then
    return --Stops the function from going further
else
... --Code, if the conditional (cooldown in this case) is met
end

try printing number. the first script will print 12 because the function returned 12; the second script will print nil since the function did not return anything

plot twist, they ARE chatgpt, the ai takeover is upon us, pack your bags lads, we getting replaced :skull:

this is hilarious he not even subtle :sob: