EasyManua.ls Logo

Commodore VIC-20 - Page 113

Commodore VIC-20
404 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...
Chapter
3:
Programming the
VIC
20
Computer
99
NESTED
SUBROUTINES
Subroutines can be nested.
That
is
to say, a subroutine can itself branch
to, or
call,
another subroutine, which in turn can call a third subroutine, and
so on. You do not have to do anything special in order to use nested
subroutines. Simply branch to the subroutine using a GOSUB statement;
each subroutine ends with a RETURN statement. VIC BASIC will
remember the correct line number for each nested return. The following
program illustrates nested subroutines:
10
REM
MAIN
PROGRAM
20
REM
YOU
CAN
DIMENSION
A
SUBROUTINE'S
VARIABLE
IN
THE
MAIN
30
REM
PROGRAM.
IT
IS
A
GOOD
IDEA
TO
DIMENSION
ALL
VARIABLES
50
REM
AT
THE
START
OF
THE
MAIN
PROGRAM.
60
DIM
A(99)
70
GOSUB
2000
80
REM
DISPLAY
SOMETHING
TO
PROVE
THE
RETURN
OCCURRED
90
PRINT"RETURNED"
190
GOTO
11210
2000
REM
FIRST
LEVEL
SUBROUTINE
2010
FOR
1-0
TO
99
2020
A(I
)
..
1
2030
GOSUB
312100
2040
NEXT
I
2050
RETURN
3000
REM
NESTED
SUBROUTINE
3010
PRINT
A(
I)
3020
RETURN
This program moves the? A(I) statement out of the subroutine and puts
it into a nested subroutine. Nothing else changes.
COMPUTED
GOSUB
STATEMENT
Since GOTO and GOSUB statement logic
is
very similar, it will not
come as any surprise
that
there
is
a computed GOSUB statement akin to the
computed GOTO statement. The computed GOSUB statement allows you
to branch to one of two or more subroutines, depending on the value of
an
index. Consider the following statement:
9121
100
ON
A
OOSUB
1090,500,5000,2300
110
When the statement on line
100
is
executed, if A = 1 the subroutine
beginning
at
line
1000
is called.
If
A = 2 the subroutine beginning
at
line
500

Other manuals for Commodore VIC-20

Related product manuals