goto labels inside functions

BlitzMax Forums/BlitzMax Beginners Area/goto labels inside functions

RustyKristi(Posted 2016) [#1]
How do I use Goto Labels inside BlitzMax functions? I tried the below but I'm getting

"Labels must appear before a loop or DefData..."

function foo()
  #startagain
   ...
   SomeCondition
   ...
     Else
       Goto startagain
end function



Brucey(Posted 2016) [#2]
Why not use a While loop and Continue?


dw817(Posted 2016) [#3]
Rusty, as a die-hard veteran of the old days and ways, it saddens me to say, using GOTO is just not good programming today.

If you add "STRICT" to the top of your code, you won't even be able to use GOTO.

Brucey is correct, you can use this, but more importantly for you to be able to EXIT one of these loops early (which I think is what you want, you can do it thissaways):
Strict
Local i

addit i

Function addit(i)
Repeat
  i:+1
  Print i
  If i=10 Then Exit
Until i=20
Print">"+i
EndFunction
Using the EXIT command you can force to exit any loop (While/Wend, Repeat/Until, For/Next), which GOTO did for you earlier.

If you want to exit an entire FUNCTION early, you can use:
If i=10 then Return
See the difference between the two different commands of Return and Exit ?

Hope This Helps !


RustyKristi(Posted 2016) [#4]
Thanks guys. Coming from B3d I'm just curious how it's done in Max because I thought most of the commands and routines would be the same.

The While method worked for me, for now. thanks.


Yasha(Posted 2016) [#5]
Specifically the error message you got in the top post is because you can do this:

#Outer
For Local x:Int = 1 to 10
    For Local y:Int = 1 to 10
        If y * x > 40 then Exit Outer  'jumps to...
    Next
Next
'...here


Works also with Continue. This feature remains available in Strict mode and replaces 99% of the remaining times when Goto was useful in B3D.


dw817(Posted 2016) [#6]
Yasha, that is not EVEN listed in BMax's help. Very nice way to skip two or more loops !

I will have to commit this to memory.


RustyKristi(Posted 2016) [#7]
Thanks. Basically there is a child function that has some loop on it after the label. So this might be the reason why I'm getting the error, but I need the label on the parent function.


TomToad(Posted 2016) [#8]
Yasha, that is not EVEN listed in BMax's help. Very nice way to skip two or more loops !

Help/Language/Program Flow/Exit and Continue.


Brucey(Posted 2016) [#9]
a child function that has some loop on it after the label. So this might be the reason why I'm getting the error, but I need the label on the parent function.

You want to jump out of one function to a label in another?

Me thinks you *really* need to reconsider the design of your program.


dw817(Posted 2016) [#10]
Ah, but Tom. It is not showing up with the EXIT command which is where I was looking:
Exit 
Description Exit enclosing loop 
Example Rem
Exit causes program flow to exit the enclosing While, Repeat or For loop.
End Rem

Repeat
	Print n
	n:+1
	If n=5 Exit
Forever
Searching NOT the command but Program Flow, yes, they show it:
Strict
Local k,j
#Label1   'loop label
For k=1 To 4
  #Label2    'another loop label (unused in this example)
  For j=1 To 4
    Print k+","+j
    If k=3 And j=3 Exit Label1
  Next
Next
Wouldn't hurt them a few characters to put that sample with the EXIT command. :)

In any case, it's definitely something useful to learn when you want to break out of a massive set of loops at once.