GOSUB
Encyclopedia
GOSUB is a command in many versions of the BASIC computer programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

. 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
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 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.

RETURN

A RETURN
Return statement
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address. The return address is saved, usually on the process's call stack, as part of the operation...

 command resumes program flow from the point at which GOSUB was invoked.

Using GOSUB too many times, as in a loop or recursively, without corresponding RETURN statements, would typically cause a stack overflow
Stack overflow
In software, a stack overflow occurs when too much memory is used on the call stack. The call stack contains a limited amount of memory, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture,...

. On the other hand, when the BASIC interpreter encounters a RETURN statement without a GOSUB it will emit a RETURN WITHOUT GOSUB error.

Computed GOSUB

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.

Support

Not all BASIC implementations support GOSUB or ON..GOSUB. For example, in FreeBASIC
FreeBASIC
FreeBASIC is a free/open source , 32-bit BASIC compiler for Microsoft Windows, protected-mode DOS , Linux, FreeBSD and Xbox....

 GOSUB is considered as deprecated in favor of SUB
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

/FUNCTION, and is disabled by default. In Visual Basic
Visual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...

, GOSUB and ON..GOSUB were removed when VB.NET was released.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK