A Better Understanding of Return?

I Already know that return Shockingly returns things, but I just want to get a better understanding of it.

Like For Example:

function Something()
  if Condition then
     return true -- What Exactly does this do?
  else
     return false -- And this
     end
end

For some reason, I can never get a good understanding of what return does, Like what is the point if it.
Can you Help?

Thanks

so in this context you could do:

local thing = Something()

if thing == true then
-- stuff
else
end

Yeah, i understand that part, but is there really anything else that i should know?

If you understand that then that is most of it, but maybe I misunderstand the question?

return allows you to give some value back to where ever you called it. For example, a simple add function:

function sum(x,y)
  return x+y
end

Can be used like this

value = sum(1,2)
-- value == 3

without return, the value would be lost once the function exits

the only other part of a return is that it ends the function

function test()
  print("This will run")
  return -- the function exits early because of this return
  print("This wont run")
end

return true and return false are the same way, just with booleans instead of integers

1 Like

In addition to returning a single value, return can also return multiple values.

local function foo()
    local number1 = math.random(0, 10)

    return number1, number1 * 3, number1 ^ 2
end

local n1, n2, n3 = foo()

In some cases return can be used as an alternative to else.

local baz = "Roblox!"

local function bar()
    if baz == "Roblox!" then
        return
    end

    print(baz.." does not equal Roblox!")
end

Yeah, then I probably was overreacting that i had missed something because all of this makes sense to me, thanks tho

1 Like

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