Gosub : Return


Syntax
Gosub MyLabel 
   
MyLabel: 
  ...
Return

Description
Gosub stands for 'Go to sub routine'. A label must be specified after Gosub, at that point the program execution continues immediately after the position defined by that label, and will do so until encountering a Return. When a return is reached, the program execution is then transferred immediately below the Gosub.
Gosub is useful when building fast structured code.

Another technique which may be used in order to insert a sub routine into a standalone program component is to use procedures. Gosub may only be used within the main body of the source code, and may not be used within procedures.

Example

  a = 1
  b = 2
  Gosub ComplexOperation 
  Debug a 
  End 
       
  ComplexOperation: 
    a = b*2+a*3+(a+b) 
    a = a+a*a 
  Return 

Syntax
FakeReturn 
Description
If the command Goto is used within the body of a sub routine, FakeReturn must be used. FakeReturn simulates a return without actually executing a return, and if it is not used, the program will crash. Note: To exit a loop safely, Break should be used instead of Goto.

Example

  Gosub SubRoutine1
     
  SubRoutine1:
    ...
    If a = 10
      FakeReturn 
      Goto Main_Loop 
    EndIf 
  Return