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 #355 background imageLoading...
Page #355 background image
Programming
28
The variables used as formal parameters in a procedure are local variables and they
are not recognised outside the
procedure
In
which they appear.
The following example may help
to
make the distinction clear Before
going
on, type
new
to
clear the computer's memory. Flfst we create a
procedure
which uses two local
variables a and
b$,
as well as aSSigning values to two normal (global) variables u and
v$.
proc
demoi
a,b$
print
a,b$
Let
u=3
Let
v$="text
ll
print
u;v$
endproc
Then we use demo:
demo 5 i
"words"
All four values are printed showing that all four variables are recognised Inside
demo.
Typing
print
u;v$
shows that both of these variables are also recognised outside the procedure. However,
typing
print
a,b$
results
In
an error because a and
b$
are not recognised outside demo. All formal
parameters are local variables, but you
can
also declare other variables
to
be
local, as
in the following example:
proc
dumbo
print
"inside
dumbo"
print
Pi
q;
r
endproc
proc
dummy
LocaL
q,r
Let
p 2
tet
q = 3
Let
r = 4
print
"inside
dummy"
print
Pi
qi
r
dumbo
endproc
If
you attempt
to
use
dummy
by
typing:
dummy
you will find that the values of
p,
q
and
r are all recognised (and therefore printed) in
dummy,
but
dumbo
does
not know the values of q and ( which are local to
dummy.
The values of local variables are not defined anywhere except in the
procedure
in which
they are declared - not even in
procedures
called from the declaring procedure. The
variable p is global and
is
recognised everyWhere
You
may
be
wondering
why local variables are necessary.
To
illustrate their usefulness,
suppose
you write a program containing several procedures that you, or someone else,
originally write for use
in
other programs.
It
is
quite possible that
two
or
more
of
these
procedures
might
use variables with the same name for qUite different purposes.
If
these
variables were global then one
procedure
could alter a value so that
It
would
be
wrong
for another In such a situation you would have to
check
all the
procedures
that you
use and,
if
necessary,
change
the names of the variables.
If,
however, the variables were
local
it
would not matter
if
they
had
the same name. Provided they were
In
different
procedures,
changing
one would have no effect on the other
Furthermore,
It
does
not matter
if
a
procedure
calls another which uses the same
name
for a variable - provided at least one of them
is
local. For example, the procedure
choose
in the section on errors, earlier
In
this chapter, declared the variable choice$ to
be
local.
This
means
that there
is
no
need to
check
whether any of the
many
procedures
called
by choose also use choice$ - the called procedures cannot
change
the value of choice$
in choose.
12184
f

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