Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why do some people use break and others return end?

Asked by 5 years ago

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!

1
break will only break from the loop, return will break out of the whole function Vulkarin 581 — 5y
0
ye User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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

0
This does not answer my question. return end does not return data and is used in a loop in the example I provided. User#21908 42 — 5y
0
I know that they are different things. User#21908 42 — 5y
0
"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. ReallyExpensive 10 — 5y
0
I already knew everything that you told me. My question was: why? User#21908 42 — 5y
0
Especially if they use return end when it is not inside of a function but just a loop. User#21908 42 — 5y
Ad

Answer this question