EasyManua.ls Logo

Sharp PC-1500 - GOSIJB and RETURN

Sharp PC-1500
169 pages
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...
A
ll
and more about Sharp P
C-
1500 at http://www.PC-1500.info
Comments, following
the
REM
key.vord, may be inserted
on
their own line
or
at
the end
of
another statement or
se
ries
of
statement
s.
Comm
ents
may
not
appe()r before or
in
the
middle
of
executable
statements.
The reason for this
is
that
when SHARP sees
the
REM
keyword it ignores
any
characters
on
th
e rest
of
the
line.
If
REM
wern used
at
the
beginning
of
a line, valid program
statements
would
be
ign
ored.
T.
GOSUB
...
RETURN
As
you
begin to design programs,.
you
\Yill
find that
you
utilize
ce
rtain
funct
ions repeatedly
wi
thin a
single
program. For example,
such
funct
io
ns
might include calculat
ing
the
area
of
a
ci
r
cle
or accepting and
ch
ecking a
nu
mber given
by
the
user. Such repetition causes duplicati
on
of
the
statements which perform the function.
To
avoid th
is
wasteful duplication, the pr
og
r
am
mer may
use the GOSUB
statement.
The
GOS
UB
s
tat
ement
allows a group
of
other
statemen
ts
, which are used
in
several places
within the program ,
to
be
set aside. This group
of
statements
is
called a
"subrout
i
ne"
(hence
the term
GOSUBi.
At
each place 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
as
i
de.
This process is kno
wn
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
statement.
The
difference, however,
is
that
before SHA
RP
begins
to
perform
the
st
ate
me
nts
of
the
subrout
ine it "remembers" where it was. When
th
e
computer
finishes performing
the
· subroutine
it
returns
to
the
point where
it
left
off
. This is known
as
11
returnin9" from a
subroutine.
But how does SHARP
discern
the
end
of
the
statements
whi
ch
form the swbroutine? The
answer
is
that
you
must
inform it
with
the RETURN
statement.
The form
of
the
GOSUB
stat•·
m
ent
is:
GOSUB line.number
where line-number
is
the
number
of
the
first line
of
the
subroutine.
The
form
of
the RETURN
stateme
nt
is
simply:
RETURN
As an example
of
a subroutine in action,
co
nsider a program
to
compute
and
co
mpare the area
of
two recta
ngl
es
given
the
length and wi
dth
of
the sides:
Program
Listi
ng:
10
REM READ IN LENGTH AND
20
REM WIDTH OF
TW
O RECTANGLES
30
FOR I = 1 TO 2
0 PAUSE "RECTANGLE
";I;
" : "
5·0 INPUT
"ENTER
LENGTH, WIDTH", L, W
60
GOSUB
200
70
IF
1=1
LETA1 = A
8 0 IF I = 2 LET A2
=A
90
NEXT I
Do
not sale this PDF
!!!
58

Table of Contents

Other manuals for Sharp PC-1500

Related product manuals