GOSUB

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: GOSUB
Abbreviation: G, O, Shift+S
Type: Command
Token code: 141/$8D-
Handling routine
in BASIC ROM:
43139–43167
$A883–A89F
List of all BASIC keywords


Remark: This article describes the BASIC command GOSUB in BASIC V2 at the Commodore 64.

Typ: Command
General Programming-Syntax: GOSUB <line>

The BASIC command GOSUB jumps to a subroutine at the indicated line number. This call is placed onto the stack. The subroutine finalizes using a RETURN command. Program execution continues at the command following the initial GOSUB command.

Many subroutines can be called consecutively, but the last one called will be the first to finish. When too many subroutines are called at once (approx. 23), the error ?OUT OF MEMORY ERROR IN line is thrown. When the line number doesn't exist in the GOSUB command, the error ?UNDEF'D STATEMENT ERROR IN line occurs.

The performance of the GOSUB gets progressively worse as the program size grows. This can be addressed to some degree by understanding the line-lookup process.

Examples[edit | edit source]

10 PRINT CHR$(147)
20 SP = 20: ZE = 3: A$ = "Good Morning!": GOSUB 1000: GOSUB 2000
30 SP = 10: ZE = 3: A$ = "I'm the Commodore 64": GOSUB 1000: GOSUB 2000
40 SP = 12: ZE = 6: A$ = "And what is your name ?": GOSUB 1000
100 END
1000 REM cursor positioning and printing
1010 POKE 211,SP :POKE 214, ZE: SYS 58640 : PRINT A$
1020 RETURN
2000 REM delay-loop
2010 FOR X=0 TO 3000: NEXT X
2020 RETURN
BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT