EasyManuals Logo

Sinclair QL User Manual

Sinclair QL
422 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
Page #104 background imageLoading...
Page #104 background image
Procedures
and
Functions
EXAMP
LE
Use
actual variable parameters,
cosL
1and
cosL2
to
receive the values
of
the variable
price from the procedure. Make the main program compute and print the
total
bill.
Program
Output
100
REMark
Vari
able
parameter
110
LET
num
=
70
120
item
3,cost_1
130
item
4,cost
2
140
LET
bi
It
=
num
+
cost_1
+
cost
2
150PRINTbili
160
DEFi ne PROCedure i
tem(num,
pri
ce)
170
IF
num
<=
3
THEN
LET
price
=
300
+
10*num
180
IF
num
>=
4
THEN
LET
pri
ce =
12*num
190
END
DEFine
448
The
parameters num and price
are
both automatically
local
so
there can
be
no problems.
The
diagrams show how information passes
from
main program
to
procedure and back
Main
Program
..
Menu numbers
prices
Procedure
Item
88
That
IS
enough about procedures and parameters
for
the present.
FUNCTIONS
You
already know how a
system
function
works.
For
example the function:
SQRT(9)
computes the
value,
3,
which
is
the square root
of
9.
We
say
the function returns the
value
3.
A function,
like
a procedure, can have one or more parameters, but the
distinguishing feature
of
a function
is
that
it
returns exactly one
value.
This means that
you
can
use
it
in
expressions that you already
have.
You
can type:
PRINT
2*SQRT(9)
and get the output
6.
Thus a function behaves
like
a procedure
with
one or more value
parameters and exactly one variable parameter holding the returned value; that variable
parameter
is
the function name
itself.
The parameters need not be numeric.
LEN("string")
has a string argument but
it
returns the numeric value
6.
EXAMPLE Re-wrlte the program
of
the last section which used price
as
a variable parameter.
Let
price be the name
of
the function.
The
value
to
be
returned
IS
defined
by
the RETurn statement
as
shown.
Program
100
REMark
FuNction
with
RETurn
110
LET
num
=
70
120
LET
biLL
=
num
+
price(3)
+
pr;ce(4)
130
PRINT
biLl
140
DEFine
FuNction
price(num)
150
IF
num
<=
3
THEN
RETurn
300
+
10*num
160
IF
num
>=
4
THEN
RETurn
12*num
170
END
DEFine
Output
448
Notice the simplification
in
the calling
of
functions
as
compared with procedure calls.
12/84

Other manuals for Sinclair QL

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Sinclair QL and is the answer not in the manual?

Sinclair QL Specifications

General IconGeneral
ProcessorMotorola 68008
Clock Speed7.5 MHz
RAM128 KB (expandable to 640 KB)
ROM48 KB
Operating SystemSinclair QDOS
Release Year1984
StorageMicrodrive tape loop
Graphics256x256 pixels, 8 colors
PortsRS-232, ROM cartridge
SoundBeeper (internal speaker)

Related product manuals