EasyManua.ls Logo

Commodore 128 - Programming Subroutines; The GOSUB-RETURN Comm Ands; The on GOTO;GOSUB Commands

Commodore 128
448 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
PROGRAMMING SUBROUTINES
The GOSUB-RETURN command
Until now, the only method ou have had to tell the computer to
jump to another part of your program is to use the GOTO
command. What if you want the computer to jump to another part
of the program, execute the statements in that section, then
return to the point it left off and continue executing the program?
The part of program that the computer jumps to and executes is
called a subroutine. Clear your computer’s memory and enter
the program below.
10 A$=SUBROUTINE:B$=PROGRAM
20 FOR J=1 TO 5
30 INPUT ENTER A NUMBER;X
40 GOSUB 100
50 PRINT B$:PRINT
60 NEXT
70 END
100 PRINT A$:PRINT
110 Z+Xt2:PRINT Z
120 RETURN
This program will square the numbers you type and print the
result. The other print messages tell you when the computer is
executing the subroutine or the main program. Line 40 tells the
computer to jump to line 100, execute it and the statements
following it until it sees a RETURN command. The RETURN
statement tells the computer to go back in the program to the line
immediately following the GOSUB command and continue
executing. The subroutine can be anywhere in the program -
including after the END statement. Also, remember that the
GOSUB and RETURN commands must always be used together
in a program (like FOR-NEXT and IF-THEN), otherwise the
computer will give an error message.
The ON GOTO/GOSUB command
There is another way to make the computer jump to another
section of your program (called branching). Using the ON
statement, you can have the computer decide what part of the
program to branch to based on a calculation or keyboard input.
4-17

Table of Contents

Other manuals for Commodore 128

Related product manuals