havenphillip Posted January 4, 2019 Share Posted January 4, 2019 What are the appropriate uses of it? I've never fully understood when and where it should be used. Quote Link to comment Share on other sites More sharing options...
catch22 Posted January 4, 2019 Share Posted January 4, 2019 A function that expects a return value. so for example... main { var x = hello(); print(x); // outputs 1 } int hello() { return 1; } Quote Coding for Christ. Link to comment Share on other sites More sharing options...
Thirsty Panther Posted January 4, 2019 Share Posted January 4, 2019 From Programming in Lua by Roberto Ierusalimschy A return statement returns occasional results from a function or simply finishes a function. There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value. For syntactic reasons, a break or return can appear only as the last statement of a block (in other words, as the last statement in your chunk or just before an end, an else, or an until). For instance, in the next example, break is the last statement of the then block. local i = 1 while a[i] do if a[i] == v then break end i = i + 1 end Usually, these are the places where we use these statements, because any other statement following them is unreachable. Sometimes, however, it may be useful to write a return (or a break) in the middle of a block; for instance, if you are debugging a function and want to avoid its execution. In such cases, you can use an explicit do block around the statement: function foo () return --<< SYNTAX ERROR -- `return' is the last statement in the next block do return end -- OK ... -- statements not reached end 1 Quote Link to comment Share on other sites More sharing options...
havenphillip Posted January 4, 2019 Author Share Posted January 4, 2019 Ok. It's confusing. I have this at the bottom of my AI script. Do I need/can I make use of any returns anywhere? function Script:UpdateWorld() if self.enabled == true then self.despawnTimer = self.despawnTimer + Time:GetSpeed()/100 if self.despawnTimer > self.despawnTime then self.mode = "dead" self:DespawnItem() end end if self.mode == "dead" then self.removeBodyTimer = self.removeBodyTimer + (Time:GetSpeed()/100) if (self.removeBodyTimer > self.removeBodyTime) then self:DespawnItem() end end end function Script:DespawnItem() self.entity:Hide() self.entity:Release() end Quote Link to comment Share on other sites More sharing options...
Josh Posted January 4, 2019 Share Posted January 4, 2019 You only need a return statement if your function returns a value, or if you want to break out of the function early. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.