ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

RETURN Command

Summary

Returns execution to the statement immediately following the GOSUB call that initiated the execution of a subroutine.

Syntax

  • RETURN

Remarks and Examples

Note: Subroutines and the GOSUB and RETURN statements are "deprecated" -- they are included for compatibility with old BASIC programs, but they are considered out-of-date. When writing a new program, you should consider using procedures (see the PROCEDURE command) and functions (see the FUNCTION command) instead, as these offer stronger program structure, better clarity through more explicit naming, and much less danger of unintentional errors and side-effects.

A subroutine is a section of code that is identified by a line number or line label, and which ends with a RETURN statement.

A subroutine is called by specifying the GOSUB statement, followed by the line number or line label of the subroutine. The GOSUB statement causes the program execution to jump to the line number or line label, and commands will be executed until a RETURN statement is encountered, at which point execution will transfer back to the statement immediately following the GOSUB statement.

The following is an example of a subroutine being defined and called in a line-numbered program:

100 PRINT "Hello"
110 Name$ = "Alice"
120 GOSUB 2000
130 Name$ = "Bob"
140 GOSUB 2000
150 END
2000 REM This is a subroutine
2010 PRINT "Hi"
2020 PRINT "Your name is "; Name$
2030 GOSUB 3000
2040 RETURN
3000 REM This is another subroutine
3010 PRINT "Tralala"
3020 RETURN

Running this program gives the following output:

Hi
Your name is Alice
Tralala
Hi
Your name is Bob
Tralala

The following is an example of a similar subroutine being defined in a program that uses line labels instead of line numbers:

Main:
    Name$ = "Alice"
    GOSUB SayMyName
    Name$ = "Bob"
    GOSUB SayMyName
    END

SayMyName:
    PRINT "Hi"
    PRINT "Your name is "; Name$
    GOSUB SayTralala
    RETURN

SayTralala:
    PRINT "Tralala"
    RETURN

Running this program gives the following output:

Hi
Your name is Alice
Tralala
Hi
Your name is Bob
Tralala

If a RETURN statement is encountered and there has been no matching GOSUB call, an error will be generated and the program will end.


See also: FUNCTION, GOSUB, PROCEDURE




Copyright 2006-2012, Kevin Matz, All Rights Reserved.