Gosubn - Sharp PC-1500A Instruction Manual

Hide thumbs Also See for PC-1500A:
Table of Contents

Advertisement

All
and
more
about Sharp PC-1500
at
http://www.PC-1500.info
Comments, folh>wing the REM keyword, may
be inserted
on
their
own line or at the
end of
another statement or
series
of statements. Comme·nts
may
not
appear before
or
in
the middle of
executable
statements. The
reason
for this
is that
when SHARP sees
the REM
keyword
it ignores
any
charocters
on
the
rest
of
the
line.
if
REM
were
used
at
the
beginning
of
a
line. valid
program
statements
would be
ignored.
T.
GOSUB ... RETURN
As
you begin to design programs,
you will
find that
you
utilize
certain
functions
repeatedly
within a
single
program.
For example, such
functions might include calcu lating
the
area
of a
circle
or
accepting
and ch.ecking
a number given by the user. Such repetition causes duplication of the
statements which perform
the
function.
To
avoid ·this wasteful duplication.
the
programmer may
use the GOSUB
statement
.
The
GOSUB
stMement
allows
a group
of
other
statements,
which
arc used
in
several ploces
within the
program
, to be set
aside.
This
group
of
statements
is
called
a
"subroutine"
(hence
the term
GOSUB).
At each ploce in the program where
the
group of statements would occur,
a GOSUB instruction is inserted.
The
GOSUB
statement instructs
SHARP
to begin executing the
group of
statements
which
have
been
set
aside_
This
process is
k
nown as "calling
a subroutine". Because
the
GOSUB
state·
ment
causes a
change
in
the
normal
sequential flow
of
execution, it
is
similar
to
the
GOTO
natement.
The
d;fference,
however,
is
that before
SHARP begins
to
perform ·the statements
of
the subroutine
it
"rcme1nbers" where
it 1,,vas.
Vv'hen the
computer
fin is hes performing
the
subroutine it returns to the
point
where
it left
off.
This is known as
"rctu
ming" from
a
subroutine.
But how does SHARP discern
the
end of
the statements which form the subroutine?
The
answer is that
you
must inform
it
with the RETURN
statement.
The form of
the
GOSUB state-
ment is:
GOSUB line-number
where
line-number
is the number
of
the first line
of
the subroutine.
The
form of the
RETURN
statement
is simply
:
RETURN
As
an
example
of
a subroutine
in
action,
consider
a
program
to
compute and
compare the
area
of two rectangles giwen the
length
and width of
the sides:
Program
Listing:
10
REM
READ
IN
LENGTH
ANO
20 REM WIDTH OF
TWO
RECTANGLES
30
FOR
I
=
1
TO
2
40 PAUSE "RECTANGLE
"
;II
;
":"
50 INPUT "ENTER LENGTH, WIDTH", L, W
60
GOSUB
200
70
IF
1
=
1
LETAl
=
A
80
IF 1=2
LETA2=A
90
NEXT I
Do not sale this PDF
!!!
58

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents