We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exit a subroutine half way through
#1
Hi there:

Just not sure if this is possible with small basic.

Been trying to exit a subroutine half way through but so far does not have much success, any ideas ?

All help is much appreciated.

Regards
Reply
#2
(translated by Google translator)

Hello, Yumda.  Shy

At the very end of the subroutine, I insert some necessary line of code or an empty operation, like this: "x = x".
Before this line I insert a transition label, for example: "subName_Exit:".

Now I can jump to the end of the subroutine from anywhere in the code to exit it.

' ---
Sub Test
  ...  ' any code

  Goto subTest_Exit

   ... ' any code
  subTest_Exit:
  x = x
EndSub
' ----
[-] The following 1 user Likes AbsoluteBeginner's post:
  • litdev
Reply
#3
Either Goto as AB showed (don't need the x=x though) or encase unwanted code in If ("False").  Note you must keep the GoTo jump within the same subroutine.

Code:
Test1()
Test2()

Sub Test1
  TextWindow.WriteLine("Used code 1")
  Goto subTest_Exit
  TextWindow.WriteLine("Unused code 1")
  subTest_Exit:
EndSub

Sub Test2
  TextWindow.WriteLine("Used code 2")
  If ("False") Then
    TextWindow.WriteLine("Unused code 2")
  EndIf
EndSub
[-] The following 1 user Likes litdev's post:
  • AbsoluteBeginner
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)