return in a for next loop

Blitz3D Forums/Blitz3D Programming/return in a for next loop

b32(Posted 2006) [#1]
If I have a function, with a loop in it, can I use return from the loop ? Or should I exit the loop first and then use return ?


GfK(Posted 2006) [#2]
You can use return, although I'd avoid gosub/return, really.


b32(Posted 2006) [#3]
Me too, I use return to exit the function:
Function ScanTextures()

  for i = 0 to maxtextures
   if texture(i) = 0 then return 0
  next

  .. some other stuff ..

  return 1

End Function

But thanks, so that means I don't have call 'exit' first.


GfK(Posted 2006) [#4]
Ah I see.

Basically as soon as your code encounters a 'Return' in that context, it exits the function completely and instantly.


Subirenihil(Posted 2006) [#5]
Return (value) exits a function immediately and returns a value if specified (the value is normally an int, but can be a float or string IF you specify in the Function <function name><return type(not required)>(<parameters> ) in which <return type> is one of the following:
% or none allows an Int to be returned
# allows a Float to be returned
$ allows a String to be returned
.customtype allows a custom type to be returned

GoSub is a bad way to do things - I don't know why GfK brought it up. Use Functions. Yes, Return can exit a For/Next or Repeat/Until loop within a function - no problem.

Blitz is a very robust language, it's a lot harder to crash than other languages. Go ahead and try things. (Don't use WritePixelFast to draw off of an image, VERY BAD things can happen - like total computer lockup. And don't make endless loops - always add an escape route like "If KeyHit(1) Then Exit") If you have questions, look in the help. (Yes, I looked and the help doesn't say anything about your question. So yes, coming here was a good thing to do.)

Good luck programming! Have fun!



Just out of curiousity...why do you ask?


Graythe(Posted 2006) [#6]
It can be done - but you should exit the for next loop beforehand.

Function ScanTextures()

for i = 0 to maxtextures
if texture(i) = 0 then exit
next
If i<maxtextures+1 then
Return
End If

End Function


b32(Posted 2006) [#7]
Thanks, so that would mean I should use exit before a return. The reason for my question is that, in my current project, I'm exiting a lot of loops, mostly in this form:
test = 0
For x = 0 to 100
for y = 0 to 100
  if p[x, y] = 0 then test = 1: exit
next
if test = 1 then exit
next
if test = 1 then return

Say, when testing all vertices of a mesh. So I wondered if it's possible to just call return, without calling exit. In other versions of basic (don't remember which one), this would give problems with the stack memory.


big10p(Posted 2006) [#8]
It's fine to use Return inside a loop - really! ;)


Beaker(Posted 2006) [#9]
Agreed, exiting loops isn't necessary.


Subirenihil(Posted 2006) [#10]
No, exiting loops is unnecessary.


Curtastic(Posted 2006) [#11]
no, exiting loops isn't not unnecessary!

I actually sometimes purposefully put code into a function so I can Return out of any amount of loops.


Graythe(Posted 2006) [#12]
Well I'll be jiggered. Out of curiosity I tried it (using return to exit a function within a loop) and it all seems to work with no memory loss. I'd probably never actually use that in anger as it goes against the grain - but it does display (again) what well designed thing B3D is. So my humble apologies for the bum steer and thanks for putting me right. How did you guys figure out that exit within a loop within a function is not needed?


GfK(Posted 2006) [#13]
How did you guys figure out that exit within a loop within a function is not needed?
Kinda known it for nigh on 20 years.

Used to code in Amstrad BASIC and learned early on that as soon as your code hits a 'Return', no code beyond it gets executed.