What is returning?

I do not understand returning in a script, yes, i have searched on the developer hub, but i cannot find anything, (if you do, please link me!), and i have watched tutorials but none of them seem to be clear.

Peasfactory’s channel doesnt really explain functions and returning that well.

What happens here?

local function foo()
    print("Hello!")
    return 0
end

foo()

What is returning? Can someone please give me a specific and detailed explanation on returning, as my mind is blank when it comes to this.

(The code was copied from Peaspod, obviously)

So, when the function is run, it would return 0, but you would not be able to get what was returned because you are not placing it and defining it. so the code would look more like

local function foo()
    print("Hello!")
    return 0
end

local hello = foo()
print(hello) --Prints 0

Basically it makes it easier to return a single value. This can also be used in a modulescript but thats a different story.

Hope this helps!

So basically, if i return 0, the print statement will print 0 because it has returned to 0?

I dont really get this.

The classic example is usually addition.

local function add(x, y)
    return x + y
end

print(add(4, 10))

As you know, x and y become 4 and 10 respectively. The return means that add will evaluate to whatever was returned. In this case, 14 was evaluated, so assigning add(4, 10) to a variable would run the function and then give you 14 in that variable.

Hello. I think the “Returning values” section of this article might help you understand it. If after re-reading this as well as others’ posts you still don’t fully get it, try to experiment with return. That’s the best way to learn!

So when ur code meets the word return, it will stop every loop inside the function and just return the value. Why would I do that? - u can save the answer(return) of the function inside of variable, for example:

local myNumber = foo()

So the value of variable myNumber will be 0

Okay so, when you call a function like you have here, the code will go back and run through the whole code. This obviously makes it so, when you use it multiple times, you dont have to copy and paste.

So now, obviously when you run a function, you need something to return. So, you need to return a value for the function, or nothing will happen. Functions can also be used without returns, for example changing the colour of a part does not require a value to be returned.

So, if you return 0, thats like saying
local value = 0, because the function has run through and the value has been returned.

local function test()
  print("This does not require a return")
  script.Parent.Colour = "Really Red"
end

local function test2()
  local alpha = 1
  local bravo = 2
  return alpha + bravo
end

test()
local addition = test2
print(addition) --prints 3, because 1 + 2 = 3

Yep

Here’s what i gathered from the developer hub:

If a sum was written inside, returning will give the answer of the sum?

local function addNumbers(num1, num2)
local result = num1 + num2
return result
end
--code from developer hub

addNumbers(10, 12)

So it will return 22?

@JamesJohansson

Yes it will, but it wont store it anywhere.

1 Like

Just figured out what i needed to do. Thanks for the cooperation!

I have another question:

if i do:

function bool_check()
    if player.bool == true then
    return false
end

whats the difference?

function bool_check()
    if player.bool == true then
    bool = false
end

(i am not sure if i have even done it properly)

In ur case there is no difference, but lets look at this example:

local function blah()
   for i, v in pairs(idk:GetChildren()) do
       if v:IsA("Traitor or sth") then
          return true -- we found a traitor, now lets punnish him and end this loop
       else
           print("v is not a traitor lets move on")
       end
   end
end      
local truth = blah()
if truth == true then
   print("Ur done my friend!")
else
   print("Lets start a new round...")
end

why is here return useful? If I would just changing a variable instead of returning, the loop would continue and at the end the variable would probably end up on not traitor thing (eventhough v was eqaul to the traitor once)

1 Like

It’s also being used for for-loops checking. Also anything under return will not run. So it will not return false if v == Entry, the function will terminate and return the result which is true.

local Arr = {"Apple", "Banana", "Orange"}

function CheckIsEntryValid(Entry)
   for i,v in pairs (Arr) do
         if v == Entry then
             return true
         end
    end
    return false
end

CheckIsEntryValid("Apple") -- true
CheckIsEntryValid("lol") -- false
3 Likes

The difference is that in the first script you’re basically turning bool_check into a variable that equals false, and in the second script you’re creating a new variable that equals false.

So if you do

print(bool_check()) 

In the first script it would print false, but if you do that in the second script it won’t. In the second script you would have to do

print(bool)

Instead.