?RETURN WITHOUT GOSUB ERROR
Apple Apple II
Severity: MinorWhat Does This Error Mean?
?RETURN WITHOUT GOSUB ERROR means the program executed a RETURN statement without a matching GOSUB. BASIC reached a RETURN but has no subroutine call on the stack to return to. This usually means program flow fell through into a subroutine that should only be reached via GOSUB.
Affected Models
- Apple II
- Apple II Plus
- Apple IIe
- Apple IIc
- Apple IIGS
- AppleWin emulator
Common Causes
- Program flow fell through into subroutine code without a GOSUB call
- Missing END or STOP before the subroutine section
- GOTO jumped into the middle of a subroutine
- Subroutine accidentally runs twice — the second RETURN has no matching GOSUB
- Stack corruption from deeply nested GOSUB calls
How to Fix It
-
Add an END statement before your subroutine section.
If your main program ends at line 200 and subroutines start at line 500, add END at line 200. Without END, BASIC runs straight from line 200 into line 500 and hits RETURN without a matching GOSUB.
-
Make sure every subroutine is only reached through GOSUB, never GOTO.
GOSUB pushes a return address on the stack. GOTO does not. If you GOTO a subroutine, the RETURN at the end has nowhere to go back to.
-
Check for missing RETURN statements that cause one subroutine to fall into another.
If subroutine A ends at line 520 without RETURN, and subroutine B starts at line 530, then calling A will run both A and B. B's RETURN then returns from A's GOSUB, and A's caller gets confused.
-
Use LIST to trace the program flow and find where the extra RETURN is.
LIST the subroutine section and check that every subroutine has exactly one RETURN. Also check that no GOTO from the main program jumps into the subroutine area.
Frequently Asked Questions
What is the difference between GOSUB and GOTO?
GOTO jumps to a line and continues from there — it never comes back. GOSUB jumps to a line but remembers where it came from, so RETURN can bring the program back to the line after the GOSUB.
How many nested GOSUB calls can Applesoft BASIC handle?
Applesoft BASIC uses a stack for GOSUB return addresses. The stack is limited, and very deep nesting (dozens of levels) can cause an OUT OF MEMORY error. In practice, 10-20 levels of nesting work fine for most programs.