Syntax
Runs the subroutine that starts at a label, up to its RETURN, then continues after the GOSUB.
GOSUB <label>
<label>:
<statements>
RETURNAlso 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
ENDbefore the labels does no harm. A label without RETURN continues into the next label, whose RETURN then goes back. GOSUBto 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 anENDon its own line, or from another subroutine that waits. ON … GOSUBis not supported: use SELECT with one GOSUB per CASE.ENDin a subroutine stops the whole program. Inside a FUNCTION,RETURNleaves the function.
Example
score = 10GOSUB AddBonusGOSUB AddBonusConsole(Str(score)) ' 30ENDAddBonus: score = score + 10RETURNIn the manual : Control flow