EasyManua.ls Logo

AMSTRAD cpc 6128 - Page 45

AMSTRAD cpc 6128
520 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...
....
you
can
see a number
oflines
which have to be repeated
at
various points in
the
program, for example
the
section from line 260 to 31
0.
Let's make
that
section into
a sub-routine
and
add the instruction
RE
T
URN
at
the
end. Then, we'll call the
sub-routine using
the
command
GOSUB
260 whenever we
want
to use it. The
program now looks like this:
10
a=2
15
GOSUB
260
80
I
90 a=5
95.GOSUB
260
160
I
170 a=8
175
GOSUB
260
240
I
250 a=9
255
GOSUB
260
256
END
257 I
260
PRINT
"here
is
the";a;"times
tabLe"
270
FOR
b=1
TO
12
280 c=a*b
290
PRINT
a;"x";b;"=";c
300
NEXT
310
PRINT
315
RETURN
See how-much-tedious_typing we've
sayed_ours~lyeslWell
JiesigIlJl(L~mb:l'()utine§
are
a principal
part
of computing. They lead to 'structured' programs, and develop good
programming habits.
Always
bear
in
mind when writing sub-routines,
that
you
do
not necessarily have to
Jump into'
the
sub-routine
at
the
same point, i.e. its beginning. A sub-routine written
from lines 5
00 to
800
can be called by: G 0
SUB
5 0
0,
or G 0
SUB
640, or G 0
SUB
790.
Note
in
the
above program,
that
the instruction
END
is used
in
line 256. Otherwise
the
program would
naturally
continue after line 2 5 5 , and would carry
outline
2 60,
which is NOT required unless called by
GO
SUB.
Chapter 1 Page
32
Foundation Course

Table of Contents

Related product manuals