GOSUB...RETURN Statements ---------------------------------------------------------------------------- Action Branch to, and return from, a subroutine. Syntax GOSUB { linelabel1 | linenumber1} . . . RETURN -{ linelabel2 | linenumber2}- Remarks The GOSUB... RETURN statements use the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- linelabel1, linenumber1 The line label or line number that is the first line of the subroutine. linelabel2, linenumber2 The line label or line number where the subroutine returns. Note BASIC's SUB and FUNCTION procedures provide a better-structured alternative to GOSUB... RETURN subroutines. In addition to RETURN with no argument, BASIC supports RETURN with a line label or line number. This allows a return from a subroutine to the statement having the specified line label or number, instead of returning to the statement after the GOSUB statement. Use this line-specific type of return with care. You can call a subroutine any number of times in a program. You also can call a subroutine from within another subroutine. How deeply you can nest subroutines is limited only by the available stack space (you can increase the stack space with the CLEAR statement). Subroutines that call themselves (recursive subroutines) can easily run out of stack space. RETURN with a line label or line number can return control to a statement in the module-level code only, not in procedure-level code. See the example program. A subroutine can contain more than one RETURN statement. A simple RETURN statement (without the linelabel2, linenumber2 option) in a subroutine makes BASIC branch back to the statement following the most recent GOSUB statement. Subroutines can appear anywhere in the program, but it is good programming practice to make them readily distinguishable from the main program. To prevent inadvertent entry into a subroutine, precede it with a STOP, END, or GOTO statement that directs program control around the subroutine. Note The preceding discussion of subroutines applies only to the targets of GOSUB statements, not procedures delimited by SUB statements. For information on entering and exiting SUB procedures, see the entry for the CALL statement (BASIC procedures). See Also RETURN, SUB Example The following example shows the use of the GOSUB and RETURN statements. Note the END statement before the module-level subroutine. If it is not present, the PrintMessage subroutine will be executed after the return to Label1. BASIC will generate the error message RETURN without GOSUB. CLS GOSUB PrintMessage PRINT "This message is in module-level code" GOSUB Sub1 PRINT "This line in module-level code should be skipped." Label1. PRINT "This message is back in module-level code" END PrintMessage. PRINT "This program uses the GOSUB and RETURN statements." PRINT RETURN Sub1. PRINT "This message is in Sub1." GOSUB Sub2 PRINT "This line in Sub1 should be skipped." Label2. PRINT "This message is back in Sub1." RETURN Label1 Sub2. PRINT "This message is in Sub2." RETURN Label2 ' Cannot return from here to module-level ' code-only to SUB1.