User Tools

Site Tools


ugbasic:user:kw:gosub
Translations of this page:


ugBASIC User Manual

GOSUB ✓

SYNTAX

   GOSUB label

PURPOSE

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.

EXAMPLE

  GOSUB leggiTasti

See also the following example files:

ABBREVIATION

 Gs

AVAILABLE ON

SEE ALSO

ANY PROBLEM?

If you have encountered a problem using this command, if you think there is a bug or the explanation is unclear, please open an issue for this keyword on GitHub. Thank you!

POWERED BY