Q(uick)BASIC Statement: RETURN
Quick View
RETURN
A control flow statement that returns control from a subroutine
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- GOSUB line1
⋮
RETURN [line2]
Description/Parameter(s)
line1 | The label or line number of the first line of the subroutine. |
line2 | The label or line number where the subroutine returns. |
- If you don't supply a label or line number for RETURN, the program continues execution at the statement following the GOSUB (for subroutine calls) or where an event occurred (for event handling). See the ⮜ ON ⮞ keyword for information about event-handling statements.
- SUB and CALL statements provide a better alternative to GOSUB subroutines.
Example
FOR i% = 1 TO 20
GOSUB Square
NEXT i%
END
Square:
PRINT i%, i% * i%
RETURN
See also:
Syntax
- RETURN [{linelabel | linenumber}]
Description/Parameter(s)
- If the argument is omitted, RETURN continues execution where an event occurred (for event handling) or at the statement following the GOSUB (for subroutine calls)
- linelabel or linenumber, if used, must identify a line in the module-level code to execute next
Without a line label, RETURN continues execution where an event occurred (for event handling), or at the statement following the GOSUB (for subroutine calls). GOSUB and RETURN without a line label can be used anywhere in a program, but the GOSUB and corresponding RETURN must be at the same level.
The linelabel or linenumber in the RETURN statement causes an unconditional return from a GOSUB subroutine to the specified line. RETURN with a line label or line number can only return control to a statement in the module-level code.
A RETURN statement cannot be used to return control to a calling program from a subprogram defined by SUB. Use EXIT SUB.
Note: | BASIC's SUB procedures provide a better structured alternative to GOSUB subroutines. |
Example
See the ⮜ GOSUB statement programming example ⮞, which uses the RETURN statement.
See also:
Syntax
- RETURN [{linelabel | linenumber}]
Description/Parameter(s)
linelabel or linenumber | Identifies a line in the module-level code to execute next. |
If the argument is omitted, RETURN continues execution where an event occurred (for event handling) or at the statement following the GOSUB (for subroutine calls). |
Usage Notes
- Without a line label, RETURN continues execution where an event occurred (for event handling), or at the statement following the GOSUB (for subroutine calls). GOSUB and RETURN without a line label can be used anywhere in a program, but the GOSUB and corresponding RETURN must be at the same level.
- The linelabel or linenumber in the RETURN statement causes an unconditional return from a GOSUB subroutine to the specified line. RETURN with a line label or line number can return control to a statement in the module-level code only, not procedure-level code.
- A RETURN statement cannot be used to return control to a calling program from a procedure defined by GOSUB. Use EXIT SUB.
- BASIC's SUB procedures provide a better-structured alternative to GOSUB subroutines.
Example
This example uses the GOSUB and RETURN statements to branch to and return from a subroutine with and without using linelabel statements.
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 illustrates use of 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 main program - only to SUB1.
See also: