Syntax

Runs the subroutine that starts at a label, up to its RETURN, then continues after the GOSUB.

GOSUB <label>
<label>:
  <statements>
RETURN

Also written : RETURN

Parts

<label>The name of the subroutine, written <label>: alone on its line. Case does not matter.
<statements>The body of the subroutine.

Good to know

  • A subroutine shares every variable of the program. It has no parameters and no local variables: write a FUNCTION when you need them.
  • A subroutine has one RETURN, at its end. An early RETURN inside an IF breaks the program: on one line it stops with an error; in a block IF the compiler cuts the subroutine there and the rest of the program is lost without a message. Put the rest of the subroutine inside the IF instead.
  • The main program never runs into a subroutine by accident, so a missing END before the labels does no harm. A label without RETURN continues into the next label, whose RETURN then goes back.
  • GOSUB to a missing label is a compile error: "Unknown BASIC label". A label that ends a chain without RETURN is one too: "BASIC label … requires RETURN".
  • A subroutine that calls Sync(), Wait() or YieldTask() waits for frames. It is allowed in a program with a DO loop, in one with an END on its own line, or from another subroutine that waits.
  • ON … GOSUB is not supported: use SELECT with one GOSUB per CASE. END in a subroutine stops the whole program. Inside a FUNCTION, RETURN leaves the function.

Example

score = 10GOSUB AddBonusGOSUB AddBonusConsole(Str(score))   ' 30ENDAddBonus:  score = score + 10RETURN
In the manual : Control flow