{{htmlmetatags>metatag-robots=()
metatag-title=(GOSUB | ugBASIC User Manual)
metatag-keywords=(ugBASIC,Commodore 64,Commodore PLUS/4,ZX Spectrum)
metatag-description=(Manual page for GOSUB)
metatag-media-og:image=(:ugbasic:logo-ugbasic-fb.png)
metatag-og:title=(GOSUB | ugBASIC User Manual)
metatag-og:description=(Manual page for GOSUB)
}}
====== 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:
* ''[[https://github.com/spotlessmind1975/ugbasic/tree/main/examples/control_returning_01.bas|control_returning_01.bas]]''
* ''[[https://github.com/spotlessmind1975/ugbasic/tree/main/examples/control_returning_02.bas|control_returning_02.bas]]''
==== ABBREVIATION ====
Gs
==== AVAILABLE ON ====
==== SEE ALSO ====
[[RETURN]] [[POP]]
==== ANY PROBLEM? ====
If you have encountered a problem using this command, if you think there is a bug or the explanation is unclear, please [[https://github.com/spotlessmind1975/ugbasic/issues/new?title=ISSUE ON GOSUB|open an issue]] for this keyword on GitHub. Thank you!===== POWERED BY =====
[[:ugbasic:user:index|{{ :ugbasic:user:logo-ugbasic.png?nolink&600 |}}]]