System Specification for C65 | Fred Bowen | March 1, 1991 |
GOSUB line
This statement is like the GOTO statement, except that the computer remembers from where it came. When a line with a RETURN statement is encountered, the program jumps back to the statement immediately following the GOSUB. The target of a GOSUB statement is called a subroutine. A subroutine is useful if there is a section of the program that can be used.by several different parts of the program. Instead of duplicating the section over and over, it can be set up as a subroutine and called with a GOSUB statement from different parts of the program. This also make.the main part of your program much more readable. See also the RETURN statement.
Variables are shared with the main program and all subroutines. You can pass information to, and get information back from, subroutines by using variables as messengers.
GOSUB statements can be nested. That is, one subroutine can call another subroutine, and the computer automatically keeps track of all the calls. It's important not to jump into or out of subroutines, since this can confuse the computer. If too many GOSUBs are nested (usually cause by jumping out of them) an 'OUT OF MEMORY' error is reported because the computer ran out of room to keep track of all the calls.
10 DIR : GOSUB 100 show directory, check status 20 GOSUB 200 print gap 30 LIST "PROGRAM": GOSUB 100 show listing, check status 40 GOSUB 200 print gap 50 etc... 90 END 99: 100 REM SUBROUTINE TO CHECK DISK STATUS 110 IF DS THEN GOSUB 200: PRINT "DISK ERROR: ";DS$ 120 RETURN 199: 200 REM SUBROUTINE TO PRINT A SPACER ON THE SCREEN 210 PRINT 220 FORI=1TO39:PRINT"-";:NEXT 230 PRINT 240 RETURN
GOTO line_number GO TO line_number
After a GOTO statement is executed, the next line to be executed will be the one with the line number following the word GOTO. When used in direct mode, GOTO line number allows starting of execution of the program at the given line number without clearing the variables.
10 PRINT"COMMODORE" 20 GOTO 10The GOTO in line 20 makes line 10 repeat continuously until STOP is pressed.
This page has been created by Sami Rautiainen. | |
Read the small print. | Last updated April 07, 2006. |