GOSUB is a command in many versions of the BASIC computer programming language. A GOSUB statement jumps to a line elsewhere in the program. That line and the following lines up to a RETURN are used as a simple kind of a subroutine without (sometimes with) parameters or local variables.
The GOSUB command may be used to emulate subroutines in a BASIC dialect that does not support subroutines in its syntax. GOSUB is convenient for performing the same function several times in a BASIC program without duplicating the code.
Using GOSUB too many times, as in a loop or recursively, without corresponding RETURN statements, would typically cause a stack overflow. On the other hand, when the BASIC interpreter encounters a RETURN statement without a GOSUB it will emit a
RETURN WITHOUT GOSUB error.
A computed GOSUB statement,
ON...GOSUB, exists in some BASIC dialects. The syntax of the statement is
ON x GOSUB line1, line2, ... Computed GOSUB branches to one of several destinations based on the value of x. RETURN commands return program flow to the statement following ON..GOSUB.
Not all BASIC implementations support GOSUB or ON..GOSUB. For example, in FreeBASIC GOSUB is considered as deprecated in favor of SUB/FUNCTION, and is disabled by default. In Visual Basic, GOSUB and ON..GOSUB were removed when VB.NET was released.
|This programming language–related article is a stub. You can help Wikipedia by expanding it.|