How do you use "return"

I’ve always seen scripts like this

local function aRandomFunction()
  return "something"
end

but never know what return does, can someone explain to me how to use them and what do they do? Thank you so much!

2 Likes

Ok so, return basically does this.

function a(n:boolean)
    if n then
        return "yes"
    end
    -- if n was true, the function would stop because it returned and it would not go to the rest of the code.

    return "no"
end

print(a(true)) -- "yes"
print(a(false)) -- "no"
function getSum(a,b)
    return a + b
end

print(getSum(2.5,5)) -- 7.5
print(getSum(10,2)) -- 12
3 Likes

Like Calvin said,

return just returns information. It’s a way for a function to give back information. If you want to calculate something like

local function GetSpeedOfCar(speed)
     return speed+60
end

It will always return the speed + 60.

Just a way of getting information from a function.

2 Likes

return can be used to return information.

function IsPlayerLiving(player)
if player.Character.Humanoid.Health == 0 then
return false
else
return true
end

To actually use this it’s basically just

local response = IsPlayerLiving(playerobject)
if response == true then
print("Living!")
else
print("Not living.")
end

A function is a snippet of code that runs whenever it is called.

Below is an example of a function. As you can see, the first three lines are dedicated to defining the function and what it will do when something calls it. Line 5 actually calls the function, prompting the function code to run.

function hello()
  print("Hello")
end

hello()

--[[
Output:
Hello
]]

The reason why functions are so useful is because they allow for reusability. We can change the above example to call the function multiple times as so:

function hello()
  print("Hello")
end

hello()
hello()
hello()

--[[
Output:
Hello (x3)
]]

We have called the function three times, so “Hello” was printed out three times. This is a simple example, but if you had more complex code that you wanted to reuse multiple times, you could wrap it in a function and call the function whenever it’s necessary. This improves readability in your code, and it’s good practice when you’re programming imperatively. You never want to repeat yourself in your code. Use a function and call it multiple times, don’t repeat the same code!

In this next example, we use arguments and parameters to show how you can make your code reusable for even more scenarios.

function add(a, b)
  print(a + b)
end

add(4, 6)
add(9, 12)

--[[
Output:
10
21
]]

As you can see, the function add has two parameters, a and b. Parameters are essentially temporary variables that only work within the scope of the function. You won’t be able to use these variables outside of the function. On line 5, the numbers 4 and 6 are passed in as arguments to the add function. add will now know that in this case of running the function, the parameter a should be 4 and the parameter b should be 6. add will then print the added result of the two passed in parameters. For 4 and 6, this should be 10. On line 6, 9 and 12 are passed in, which should print out a value of 21.

Now, to answer your question, what is return and how can you use it?

Well, sometimes it’s not very useful for your function to simply just print something out. In some cases, you may want to use a function compute some data and use that data elsewhere in your code. So far, we have not been able to use the “results” of the functions we’ve created. When we added 4 and 6 together, we simply just printed out 10. Outside of our function, we cannot use that data anywhere because it’s local to the function itself. In many cases of programming, you will want to pass in data to a function, allow the function to manipulate it, and receive back the manipulated data. This is where return comes in handy. Let’s see an example.

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

local firstAddition = add(4, 6)
local secondAddition = add(9, 12)

print(firstAddition)
print(secondAddition)

local thirdAddition = add(firstAddition, secondAddition)

print(thirdAddition)

--[[
Output:
10
21
31
]]

This time, our print statement within add is replaced with this interesting command called return. On line 5, instead of simply calling the function, the function is called and the result is put into a variable called firstAddition. Now, we have the result of the function saved! This is because add passes back the data that it has manipulated to where it was called from with return. The same logic applies to secondAddition, which is a variable that stores the addition of 9 and 12 as called from the add function. To show how powerful functions are, I’ve added a third variable called thirdAddition, which stores the addition of firstAddition and secondAddition. These are all printed out to the output for you to see them.

Hopefully this explanation helps. Let me know if you have any questions.

2 Likes

The return keyword, as the name specifies, returns something (or nil) from a function.
Let’s say you have a complicated math equation, for instance, the quadratic equation (used to get the value for 𝑥) (-b ± √(b² - 4ac)) / 2a, now, if you want to calculate the values for 𝑥 you can do like so

local a = 1
local b = 5
local c = 6
local x1, x2 = (-b + math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2), ((-b - math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2)
print(x1, x2) -- -2, -3

Now as you can see, it’s very long and hard to type. Let’s say you need to use this formula multiple times in the script, you obviosusly can’t write all of that every time, here’s where return comes to help, you can just make a function with the parameter a, b and c

local function _getValueOfX(a, b, c)
    
end

Now let’s make this function return 𝑥1 and 𝑥2, here’s how to do so.

local function _getValueOfX(a, b, c)
    local x2, x2 ((-b + math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2), ((-b - math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2)

    return x1, x2
end

Or simplify it a bit.

function _getValueOfX(a, b, c)
	return ((-b + math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2), ((-b - math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2)
end

Now, let’s use the numbers form the first code block

local function _getValueOfX(a, b, c)
	return ((-b + math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2), ((-b - math.sqrt(math.pow(b, 2) - 4 * a * c)) / 2)
end

local a = 1
local b = 5
local c = 6
local x1, x2 = _getValueOfX(a, b, c)
print(x1, x2) -- -2, -3

I hope this helped you understand, if you have any other question feel free to ask.

Return lets you get the “result” of a function. When you call a function, you can make it a variable and that variable equals whatever you return.

For example

local function NextNumber(number)
 return number + 1
end

local number = NextNumber(1) 
Print(number)  -- 2

Its basically the same as doing this

local number = 1

local function NextNumber()
 number = number + 1
end

NextNumber()
print(number)

The only difference is that it makes the code simple to comprehend and shortens your overall code. Without it, you may not be able to do certain things or have to create unnecessary variables or even redundant functions.

Returns information and stops code

Like this:

local function AddNumbers(n1, n2)
return n1 + n2
print('I went past the barrier!') -- Does not run and does not error
end

print(AddNumbers(1, 2)) -- 3, but doesn't print "I went past the barrier!"

the reason it does run the addition print is because its running inside a function

if we take it out like this:

return

print('e')

then e will not be printed and cant execute codes any further in the script without doing something outside the scrript

Return is a keyword in programming that is used to indicate what a function should output. When a function is called, it will execute the code within it and then return a value. This value can then be used in other parts of the program.

For example, in the above code, aRandomFunction() will execute the code within it and then return the string “something”. This value can then be used in other parts of the program, such as assigning it to a variable or using it in a comparison statement.

Return statements can also be used to indicate early exits from a function, such as when an error occurs. This can be useful for making sure code only executes when certain conditions are met, such as a valid value being given as an argument.

Both you and Calvin made me understand return, thank you so much! (Also thanks to the people who contributed to this post :smiley: )

2 Likes

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