Breaking out of a Sub?

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
everest
Posts: 96
Joined: 31 May 2010, 9:01 AM

Breaking out of a Sub?

Post 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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Breaking out of a Sub?

Post 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.
- Don Kinzer
everest
Posts: 96
Joined: 31 May 2010, 9:01 AM

Post 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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
everest
Posts: 96
Joined: 31 May 2010, 9:01 AM

Post by everest »

Ahh yea, I mis-read your posting. PERFECT! Thank you!

-Jeff
Post Reply