Return from sub from IF statement

Dom1132

New member
Hello,

Im trying to compress my program as much as possible as I require the space,
Ive coded up a sub routine that a repeatedly use through out the program,
However I require to return if a pin is high or continue with the for loop until the pin is high, is this possible?
Please see example below;

main: high testpoint
pause 1000
low testpoint
gosub Passcheck
goto idle


Passcheck: FOR b0 = 0 to 100
if starttest = 1 then return **** HERE IS THE ISSUE, I only want to return from sub if startest = 1, if starttest = 0 then I want to continue loop until fail
pause 50
Next b0
goto Fail
Please assist
 

Flenser

Senior Member
Test the value of starttest inside the loop and exit the loop immediately if starttest=1
Then test the value of starttest again after the loop to find out whether to goto fail or return from the subroutine.
Code:
Passcheck: 
FOR b0 = 0 to 100
  if starttest = 1 then
     then exit ; exit the for loop
  endif

  pause 50
Next b0

if starttest = 0 then
  goto Fail
endif

return
 

AllyCat

Senior Member
Hi,

The implication of your code is that you are exiting the subroutine with a GOTO instead of a RETURN, which at least to purists is an absolute "NO NO". We really need to know what is required to happen at/after the "fail", but you mustn't use any more RETURNs because the stack (pointer) is now corrupted.

IMHO, depending on what you're trying to achieve, your options are, either 1) "fail" should be a "Restart" to reset the Stack and/or to report the "error", or 2) you should Exit the subroutine via a RETURN, with a "fail" flag set (for processing within the higher level of the program), or 3) you should process the whole "fail exception error" within the Subroutine, or 4) you shouldn't use a Subroutine at all ! Typically you might use a Macro instead, but that is going to increase the Program size, not reduce it. :(

Cheers, Alan.
 

lbenson

Senior Member
PassCheck:
If starttest <> 1 then
' do stuff here
endif
return

But GOTO out of a subroutine is asking for trouble.
 

inglewoodpete

Senior Member
You can have multiple (conditional) exits from a subroutine but, as others have said, never exit a subroutine with Goto.

The code structure within a subroutine can be as complex as you can handle but using Goto statements can make debugugging difficult, especially when you revisit your code at a later date. Personally, I avoid using 'Goto' like the plague I mean COVID ;) .
 

Goeytex

Senior Member
@Dom1132

There are several ways to do what you want. However since we cannot see your complete code structure there is no way to actually provide a complete solution. We cannot see the Idle or Fail routines. Neither can we see the main loop or how it is managed. Lots of Goto statements?

That being said, the code below demonstrates a way that might be useful and that will not mess up the return stack. However this would
not necessarily be my preferred method. Post your complete code to get more useful help.


Code:
main:
    high testpoint
    pause 1000
    low testpoint

    gosub Passcheck
    If B0 = 100 then Goto Fail

Idle:
      ;do stuff here
      ; Goto Main  or somewhere else

Fail:
     ; do stuff here
     ; Goto Main or somewhere else


Passcheck:
    For b0 = 0 to 100                 
        If starttest = 1 then Exit
        Pause 50
    Next b0
    Return
 
Last edited:
Top