Extrememly confused on "returning"

I would like someone to explain to me what returning does in a simple way. I’m just I really confused on what returning does. I’ve tried using the forums and multiple YouTube videos but none seems to help me and I would like to know why do people use return

3 Likes

This script here will print “a”:

local function aPrinter()
	return "a"
end

print(aPrinter())

I personally don’t use this, but I believe more advanced scripters use return to do get data from things like math (I think)

2 Likes

Functions are a way of reusing code. If you want that code to give you a result, you use return.

1 Like

Adding on to what JarodOfJupiter said, they’re used to basically get information from functions.

Here is an easy example to follow


local function AddNumbers()

return 1+1 

end
local number = AddNumbers()

print(number)


All this code is doing is creating a function, then returning the answer of 1+1.

When something gets returned, it can be used for other purposes.

In this case, I make a variable that gets the function. Since we have a return in the function, it basically gives the function a result. (In this case it’s 1+1).

Then we can call the function in a variable which will now give the variable whatever was returned.

However, if you try running this without the return, since there is nothing to be taken from the function, it won’t work

This is useful because when you start developing more complex things, instead of just repeating code, this allows you to make one function that will return something that can make code a lot easier to read and use.

3 Likes

Return basically replaces the function you called with whatever is after the return. So,

local function ReturnFunction()
    return "Hello"
end

local SayHello = ReturnFunction() --Runs the function. Since "Hello" is after the return, it replaces the ReturnFunction() with "Hello", so now SayHello = "Hello" now.
print(SayHello)

Output:
Hello

1 Like

Like everyone said, it returns a value from a function, and you can access it by making a variable (or not). Like this one:

local function HW()
     return "Hello World!"
end

local HW = HW() --Even if its setting the variable to the "HW()" function, it's still works cause you returned a value, and that value is "Hello World".
print(HW) --Now we print the variable "HW" that is reffering the function "HW()" that has the returned value "Hello World".

The return is also very helpful at ending functions!

You can just simply use return end to do that!
Ex.

 local checked = script.Parent.BoolValue

if checked.Value == true then --If the value has a check mark then..
  print("Value is true")
else --We use else to tell the opposite the "if then" statement above.
  return --We end the function if the value doesn't have the checkmark.
end
1 Like
local function func(a,b)
  return a+b
end

print(func(1,2)) --> 3 1+2 equals 3
print(func(1,1)) --> 2 1+1 equals 2
print(func(4,2)) --> 6 4+2 equals 6
func() --> nothing because the function doesn't print anything by itself.

return is like the result of the function, here’s another example:

local function Check(a, b)
      return a == b -- Is a and b the same value?
end

print(Check(1,2)) --> false 1 and 2 are not the same value
print(Check(5,4)) --> false 5 and 4 are not the same value
print(Check(2,2)) --> true 2 and 2 are the same value

1 Like

That return end is a syntax error. Just return by itself should be used instead:

if checked.Value then -- You can also get rid of the == true bit too
  print("Value is true")
else
  return
end

Or even do this if you want to:

if not checked.Value then return end
print("Value is true")
1 Like

Oh yeah! I forgot that you can just put return, I’m using a phone that’s why I forgot abt it.

1 Like