Say if I have a repeat until loop which repeats 3 times or until the players data loads. In one example I saw someone used return end if the players data saved successfully. Why do they not just use break? Hope someone can clarify this for me. Thanks!
Why do some people use break and others return end?
1 answer
Return & break are different things and should never be confused.
Return will stop whatever function that it is. Meaning that if I did:
local function ok()
local yes = true
if yes == true then
return
end
print("nice")
end
ok()
print("nice") would never run, since the function had already stopped. It's very, very crucial to note that return's purpose is for returning data back to where it was called, meaning that:
local function ok()
return 5
end
local x = ok();
x would be equivalent to 5, since the function returned that. So return stops & gives back data if put in.
Break, on the other hand would stop whatever loop it is in, and it does not return any data.
If i did:
for i = 10, 0, -1 do
if i == 5 then
break
end
end
The loop would not go farther 4, since it would stop running, or "breaking" at 5
5 comments
This does not answer my question. return end does not return data and is used in a loop in the example I provided.
I know that they are different things.
"Return will stop whatever function that it is" "Break, on the other hand would stop whatever loop it is in" I answered your question on what each does. If you put it together, putting a loop inside a function and putting a return inside the loop would break & stop the function entirely. You must've not read my answer carefully.
I already knew everything that you told me. My question was: why?
Especially if they use return end when it is not inside of a function but just a loop.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
break will only break from the loop, return will break out of the whole function
ye