Could someone explain the scope of break and return to me? I am interested to find out if they have different effects in different situations. For instance, break is often used in loops. What if I put break into a loop inside a loop? What if I put break into a loop inside of a function? Also, what kind of effects does break have on functions? I have often seen return used with functions but not so much loops. Can return be used in a loop? I have seen return break out of a function, does that mean it has farther reaching effects than break? Please help me understand this simple question. Thanks!
What kind of scope do return and break have[Solved]?
2 answers
For the return function, it returns a value from the function ex:
local function add (x,y)
return(x + y)
end
local value = add(1,1)
print(value)
the variable value would be what is returned from the function add, which in turn gets printed. By default, functions in lua have a return function at the end of the function which returns nil
The return function can also have other uses, for example:
game:GetService("UserInputService").InputBegan:Connect(function(io,gpe)
if gpe then return end
if io.KeyCode == Enum.KeyCode.C then
...
end
end)
--io is input object and gpe is game processed event
In this circumstance, the return function is ending the function abruptly if there is a game processed event, i.e. typing in chat.
The break function breaks a loop or function that it is contained within but no other loops or functions:
local function foo ()
while true do
break --breaks the while loop
end
print("ahh")
end
foo()
--------
for _,v in pairs workspace:GetChildren() do
print(v)
break --breaks the for loop
end
The break function can come in handy in multiple circumstances, for one, here is a snipet of code from StickMasterLuke's mad bloxxer.
while true do
wait(5)
contestants = {}
for _,player in pairs(game.Players:GetPlayers()) do
if player and player.Character then
local hum = player.Character:FindFirstChild("Humanoid")
if hum and hum.Health > 0 then
table.insert(contestants,player)
end
end
end
if #contestants >= 3 then
break
else
statustag.Value = "Waiting for Players"
timertag.Value = -1
end
end
This is a piece of code used to stop the main game loop from progressing any further until there are more than 3 people in the game. However,when there are more than three people in the game, the break function breaks the while loop and lets the rest of the main game loop run. Note that the break function doesn't work on the if statement the function is contained in,nor the for loop directly above it, but it does on the while loop containing it.
Addition: the scope of the return function is essentially the same as that of the break function Example:
local function foo(a,b)
return goo(a * (a + 2), b/2)
end
local function goo (a,b)
return a + b
end
print (foo(1,2))
Hopefully this helped your understanding about break and return a bit.
1 comment
I know how they work and your answer did not clarify what my question was asking. I wanted to know the scope of the break and return functions.
Return is something that gets you something from a function and break is something that stops a loop. They can be used inside each other like a function can have break if it has a loop in it and a loop can have return in a function.
1 comment
he knows what they do, he's asking about the scope. for example, if there was a loop in a loop would breaking the inner loop also break the outer loop?
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Solved by kingdom5
Scopity scope scope