Msypon Posted April 7, 2013 Share Posted April 7, 2013 When we should use return and break? Link to comment
ixjf Posted April 7, 2013 Share Posted April 7, 2013 (edited) return keyword is used to return one (or more) values from a function. break, as its name explicitly says, breaks a loop. Edited April 29, 2013 by Guest Link to comment
Tete omar Posted April 7, 2013 Share Posted April 7, 2013 The return keyword should be used to return a value in a function, or stop it (simply use the keyword with no value). The break keyword is used to stop loops (do, while, repeat). And for example: break : for i,v in ipairs{1, 2, 3, 4, 5} do -- looping table if v == 2 then -- checking if the value is "2" while looping break -- breaking/stopping loop outputDebugString("Loop broken") -- outputs to debug end end return : function outputToChat(text) -- function you can call anytime you want if text:lower():find("ixjf is nice") then -- if it found a similar word like this then return outputChatBox("That's a lie!") -- it will stop the function from outputting the text and outputs this end outputChatBox(text) -- outputting to chat box the text message you've passed to the function end Link to comment
Castillo Posted April 7, 2013 Share Posted April 7, 2013 Your break example is wrong, break has to go after your code inside the loop, like this: for i,v in ipairs{1, 2, 3, 4, 5} do -- looping table if v == 2 then -- checking if the value is "2" while looping outputDebugString("Loop broken") -- outputs to debug break -- breaking/stopping loop end end Link to comment
Tete omar Posted April 7, 2013 Share Posted April 7, 2013 Your break example is wrong, break has to go after your code inside the loop, like this: for i,v in ipairs{1, 2, 3, 4, 5} do -- looping table if v == 2 then -- checking if the value is "2" while looping outputDebugString("Loop broken") -- outputs to debug break -- breaking/stopping loop end end uhm, k, thanks dude Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now