Available on:
See also: RETURN POP

GOSUB

The GOSUB command allows you to "jump" to a specific part of the program, execute a set of instructions and then return exactly to the point where you started.

It allows you to break a program into smaller, more manageable blocks of code, improving readability and maintenance. A subroutine can be called multiple times from different parts of the program, avoiding rewriting the same code multiple times. It helps organize the flow of the program, making it clearer and easier to follow.

While GOTO allows an unconditional jump to any line of the program, GOSUB is more structured and allows a return to the starting point. In general, GOSUB is considered a more powerful and flexible tool than GOTO, as it allows for better organization of the code.

It is possible to nest subroutines, but it is important to make sure that each GOSUB has its corresponding RETURN. So, a common mistake is to forget to put RETURN at the end of a subroutine, causing unpredictable behavior of the program.

Subroutines are often implemented through functions and procedures, which offer more advanced functionality and more rigorous management of variable scope.

SYNTAX

 GOSUB label


Legend
  • id : identifier
  • type : datatype
  • v : value
  • "..." : string
  • [...] : optional

EXAMPLE

 GOSUB leggiTasti


Used in:

ABBREVIATION: Gs

Join BASIC 10Liner Contest with ugBASIC!

An interesting competition is held at the beginning of each year: the BASIC 10Liner Contest. It is possible to use ugBASIC to participate in the next "BASIC10Liner" competition, in the following categories:

  • PUR-120 - A game in 10 lines of max 120 characters (w/abbrev.)
  • EXTREME-256 - A game in 10 lines of max 256 characters (w/abbrev.)
  • SCHAU - Any program in 10 lines of max 256 characters (w/abbrev.)
In order to reduce space you can use this abbreviation for this instruction:

GOSUB ↔ Gs

Any problem?

If you have found a problem with this keyword, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO KEYWORDS