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 #103 background imageLoading...
Page #103 background image
Procedures
and
Functions
Program
Output
Program
100
REMark
LOCAL
price
110
LET
price
=70
120
item
3
130item4
140
PRINT I
price
I
150
DEFine
PROCedure
item(num)
160
LOCal
price
170
IF
num
<=
3
THEN
LET
pri
ce
=
300
+
10*num
180
IF
num
>=
4
THEN
LET
pri
ce
=
12*num
190
PRINT I
price
I
200
END
DEF
i
ne
100
REMark
Global
price
110
LET
price
=
70
120
item
3
130item4
140
PRINT I
price
I
150
DEFine
PROCedure
item(num)
160
IF
num
<=
3
THEN
LET
pri
ce =
300
+
10*num
170
IF
num
>=
4
THEN
LET
pri
ce
=
12*num
180
PRINT I
price
I
190
END
DEFine
330
48
48
The price of the lager has been altered by the procedure.
We
say
that the
vanable,
price,
is
global because
It
can be used anywhere
in
the program.
Make the procedure vanable,
price, LOCAL
to
the procedure. This means that EXAMPLE
SuperBASIC
will
treat
it
as
a special vanable accessible only within the procedure. The
vanable,
price,
In
the main program
will
be a different thing even though
it
has the same
name.
330
48
70
ThiS
time everything works properly. Line
70
causes the procedure variable, price
to
be
Internally marked
as
'belonging' only
to
the procedure, item. The other
vanable,
price
is
not affected.
You
can see that local variables are usetul things.
Output
Local variables are so useful that
we
automatically make procedure formal parameters EXAMPLE
local. Though we have not mentioned
it
before parameters such
as
num
In
the above
programs cannot interfere
with
main program variables.
To
prove
this
we
drop
the
LOCAL
statement from the above program and use
num
for
the price of lager Because num
in
the procedure
is
local everything
works.
100
REMark
LOCAL
parameter
Program
110
LET
num
=
70
120
item
3
130
item
4
140
PRINT I
num
I
150
DEFine
PROCedure
item(num)
160
IF
num
<=
3
THEN
LET
price
=
300
+
10*num
170
IF
num
>=
4
THEN
LET
pri
ce =
12*num
180
PRINT I
price
I
190
END
DEFine
330
48
70
Output
So
far
we have only used procedure parameters tor passing values
to
the procedure.
But suppose the main program wants the cost ot an item
to
be passed back so that
it
can compute the
total
bill.
We
can
do
this easily
by
providing another parameter
in
the procedure call. This must be a variable because
it
has
to
receive a value from the
procedure.
We
therefore call
it
a vanable parameter and
it
must be matched by a
corresponding variable parameter
in
the procedure definition.
VARIABLE
PARAMETERS
12/84
87

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