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

A function in computer programming can either do stuff or return stuff.

Imagine a function which plays some visual effect - this function only does stuff and doesn’t need a “return” at all.

playEffect()
print'effect played!'

Now imagine a function which gets a random player in your game.
It finds the player and returns it.
In your code you could implement it like this:

local player = getRandomPlayer()
player:Kick()

This code is supposed to kick a random player, so you create a function to find a player, and then you kick them.
getRandomPlayer needs to return this player, and after it returns, the value it returned is assigned to player.
Then, once the value from the function is in the player variable, you can kick the player.

Lua itself has a lot of functions which return something, for example tick().

local a = tick() -- a has the value returned by tick now

It returns the information that is calculated in a function. For example here.

function calculate(a, b)
	local calculatedNumber = a + b
	
	return calculatedNumber
end

local printNumber = calculate(10, 10)

print(printNumber) -- prints out 20

or

function calculate(a, b)
	return a + b
end

local printNumber = calculate(10, 10)

print(printNumber) -- prints out 20

Here is the response I gave someone else:


Returning within a function is basically sending a value to whatever called it, I guess.
For example, here is a (useless) function that adds two numbers for whatever reason:

local function add(num1, num2)
    return num1+num2
end

local added = add(5,2) --any numbers
print(added) --7


Here is the roblox article on it: https://education.roblox.com/en-us/resources/returning-values-from-functions

I’ve read all of the individual comments like 5+ times over, I still don’t get it. I’m NOT faking this whole thing! I’m still clueless. If I had to guess, return might have something to do with printing?

Regardless, I wasn’t expecting this post to blow up, nor was I expecting 15 people to try and help me. Thank you all so much for trying.

No, it has to do with assigning a value to the function… like this:

function part()
   local part = Instance.new("Part");
   return part
end
part():Destroy() -- destroy is a property of the part, since we returned the part, we can use its properties.

What do you mean by this? I thought that when making just a function and nothing else, you already would have provided it with a name ( TRNSPRNCY ), instructions on what to do,
( game.Workspace.Part.Transparency ) and the occasional parameter ( PARAMETER aka. 0.5)

Screenshot 2023-03-13 145929

I know, but if you set a variable to that function, you can edit the variable from outside the function.

Here’s an analogy to make you understand it better. Let’s say we have a part in workspace named part for convenience. To access this part in code, we normally do

game.Workspace.Part -- It is equal to the part

Now, let’s create a function where we return a part we created in the function. Remember, we can’t access the part normally because of scope.

function createpart()
   local part = Instance.new("Part")
   return part
end

local part = createpart() -- It is also equal to a part, but equal to the one CREATED in the FUNCTION

Now, createpart() is now A PART like game.Workspace.Part, because we returned a part which the function is now assigned as. Therefore, the part variable is equal to the part we created in the function.

Additional Information: Return is used for getting information from a function, which we can’t do normally because of scope. So, if we did some very complex math equation in a function and added a variable to the solution to the equation in the function. We cannot access the solution outside of the function. Thats where we have to return the variable that is the solution to the equation.

2 Likes

You can use functions two different ways.


One to return or calculate a value:

local function area(length, width)
    return length * width
end

local value = area(5, 10)
print(value) --> 50

And one to do tasks:

local function startTimer(seconds)
    for i = seconds, 0, -1 do
        print(seconds) --> 60, 59, 58, ..., 2, 1, 0
        task.wait(1)
    end
end

startTimer(60)

Return will stop the function, and pass on the returned value to whoever called it.
This means, you can use return to just stop the function.

local function startTimer(seconds)
    if type(seconds) ~= "number" then return end -- If the type of 'seconds' is not a number, then return
    -- other code
end

Exercise

Try to make a function to return the average of two numbers.
The formula is: (a + b) / 2

Unit Tests

Input → Output


5, 15 → 10
2, 6 → 4
0, 10 → 5
-10, 10 → 0
20, 0 → 10

Solution (open when finished with your code)
local function average(a, b)
    return (a + b) / 2
end
print(average(5, 15)) --> 10
print(average(2, 6)) --> 4
print(average(0, 10)) --> 5
print(average(-10, 10)) --> 0
print(average(20, 0)) --> 10

I think I might’ve figured it out. From how I see it,

Return takes a select variable out of the function/scope It’s currently in so that it can be used in the next biggest scope it’s also in, or throughout the whole script if the function it’s in is the only “function” in it’s script.

Screenshot 2023-03-13 154548

That’s now what I’m getting from this. Is it true?

In short, yes.

It is also used in ModuleScripts where you can call require(ModuleScriptHere) and get data from that modulescript, this is usually a table or a function but it can be whatever you want it to be.

Alright!!!

I will learn about Module scripts another day, and I’m sorry for all of the previous confusion. I thought that with a name called “return” it would do the exact opposite of what it actually does but from how I see it now, “return” seems more like it would “distribute”. Weird.

Thank you and thank you all :+1:

1 Like

Really, I think the name is actually ok descriptive, as it ‘returns’ the current state of the part

I mean you can always take a look into the Roblox Documentation site once you have those questions, It’s the best way to learn and get used to Roblox programming language

https://create.roblox.com/docs

Return can “return” information from functions. This is mostly used to avoid nesting.
Like changing a CFrame of a part, and many more.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.