EasyManua.ls Logo

Commodore VIC-20 - Page 37

Commodore VIC-20
308 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...
This
is
a
specialized
form
of
the
GOTO
statement,
with
the
important
difference
that
GOSUB
remembers
where
it
came
from.
When
the
RETURN
statement
(different
from
the
RETURN
key
on
the
keyboard)
is
reached
in
the
program,
the
program
jumps
back
to
the
statement
immediately
following
the
original
GOSUB
statement from
which
it
came.
The
major
use
of
a
subroutine
{GOSUB
really
means
GO
to
a
SUBroutine)
is
when
there
is
a
small
section
of
program
that
is
used
by
different
sections
of
the
program.
By
using
subroutines
rather
than
repeating the
same
lines
over
and
over
at
different
places
in
the
program,
you
can
save
lots
of
program
space.
In
this
way,
GOSUB
is
similar
in
use
to
DEF
FN:
DEF
FN
lets
you
save
space
when
using
a
formula,
and
GOSUB
saves
space
when
using
a
several-line
routine.
Here
is
an
inefficient
program
that
doesn't
use
GOSUB:
100
PRINT
"THIS
PROGRAM
PRINTS'1
110
FOR
L
-
1
TO
500
:
NEXT
120
PRINT
"SLOWLY
ON
THE
SCREEN"
130
FOR
L
=
1
TO
500
:
NEXT
140
PRINT "USING
A
SIMPLE
LOOP"
150
FOR
L
-
1
TO
500
:
NEXT
160
PRINT
"AS
A
TIME
DELAY."
170
FOR
L
=
1
TO
500
:
NEXT
Here
is
the
same
program
using
GOSUB:
100
PRINT
"THIS
PROGRAM
PRINTS'1
110
GOSUB
200
120
PRINT
"SLOWLY
ON
THE
SCREEN"
130
GOSUB
200
140
PRINT "USING
A
SIMPLE
LOOP"
150
GOSUB
200
160
PRINT "AS
A
TIME
DELAY."
170
GOSUB
200
180
END
200
FOR
L
=
1
TO
500
:
NEXT
210
RETURN
Each
time
the
program
executes
a
GOSUB,
the
line
number
and
position
in
the
program
line
are
saved
in
a
special
area
called
the
"stack,"
which
takes
up
256
bytes
of
your
memory.
This
limits
the
amount
of
data
that
can
be
stored
in
the
stack.
Therefore,
the
numberofsubroutine
return
addresses
that
can
be
stored
is
limited,
and
care
should
be
taken
to
make
sure
every
GOSUB
hits
the
corresponding
RETURN,
or
else
you'll
run out
of
memory
even
though you
have
plenty
of
bytes
free.
21

Other manuals for Commodore VIC-20

Related product manuals