Page 1 of 1

Breaking out of a Sub?

Posted: 04 June 2010, 14:37 PM
by everest
I'm creating a subroutine and there are several points where I want to break out of the routine at certain points if certain conditions are met.

In PBASIC I would simple use a RETURN inside a conditional statement. It doesn't look like ZBasic has anything like "RETURN". . .does that mean I'm going to have to create large nested conditional statement for these subs? That will certainly work, but seems very complicated.

Here's the PBASIC code I'm trying to replicate (there are many more statement like this below, so we're talking "super nesting" I think):

Code: Select all

IF ( (OOCS_Scope_Module = 1) AND (Ignore_Scope_Safety = 0) ) THEN
  IF (Scope_Status = 0) THEN
    Message(0) = "D"
    Message(1) = 0
    Message(2) = ">"
    GOSUB Send_Update
    Message&#40;0&#41; = "<"
    GOSUB Send_Update
    RETURN
  ENDIF
ENDIF

IF &#40; &#40;OOCS_Voltage_Module = 1&#41; AND &#40;Requested_Roof_State = OPEN&#41; AND &#40;Ignore_Voltage = 0&#41; &#41; THEN
  IF &#40;volts < 194&#41; THEN
    Message&#40;0&#41; = "D"
    Message&#40;1&#41; = 1
    Message&#40;2&#41; = ">"
    GOSUB Send_Update
    Message&#40;0&#41; = "<"
    GOSUB Send_Update
    RETURN
  ENDIF
ENDIF
-Jeff

Re: Breaking out of a Sub?

Posted: 04 June 2010, 14:51 PM
by dkinzer
everest wrote:It doesn't look like ZBasic has anything like "RETURN". .
The Exit Sub statement does exactly the same thing as RETURN. There are other forms of the Exit statement for early exit from For-Next, Do-Loop etc.

Posted: 04 June 2010, 15:05 PM
by everest
The compiler seems to be complaining quite a lot when I have "End Sub" in multiple locations inside a single Sub. . .a Gosub in PBASIC will jump back whenever it hits the first "Return", it doesn't seem like I can do that here.

My work-around has been to create a Boolean variable called "BREAK" and just have all parts of the sub check to see if that has been set to TRUE before executing, so once that's set, everything else gets skipped. I just though there might be an easier way to do this.

-Jeff

Posted: 04 June 2010, 15:18 PM
by dkinzer
everest wrote:The compiler seems to be complaining quite a lot when I have "End Sub" in multiple locations inside a single Sub.
You can only have one End Sub for a subroutine since it marks the actual end of the subroutine. Perhaps you misunderstood my previous post. I referred to Exit Sub not End Sub.

Posted: 04 June 2010, 15:23 PM
by everest
Ahh yea, I mis-read your posting. PERFECT! Thank you!

-Jeff