Sinclair QL Beginner's Manual page 113

Hide thumbs Also See for QL:
Table of Contents

Advertisement

Formal parameters do not have any type. You may prefer that a variable which handles numbers has
the appearance of a numeric variable and which handles strings looks like a string variable, but
however you write your parameters they are typeless. To prove it, try the following program.
Program
100 REMark Number or word
110 waiter 2
120 waiter "Chicken"
130 DEFine PROCedure waiter(item)
140
PRINT ! item !
150 END DEFine
Output
2 Chicken
The type of the parameter is determined only when the procedure is called and an actual parameter
'arrives'.
SCOPE OF VARIABLES
Consider the following program and try to consider what two numbers will be output.
100 REMark scope
110 LET number = 1
120 test
130 DEFine PROCedure test
140
LOCal number
150
LET number = 2
160
PRINT number
170
try
180 END DEFine
190 DEFine PROCedure try
200
PRINT number
210 END DEFine
Obviously the first number to be printed will be 2 but is the variable number in line 200 global?
The answer is that the value of number in line 160 will be carried into the procedure try. A variable
which is local to a procedure will be the same variable in a second procedure called by the first.
Equally if the procedure try is called by the main program, the variable number will be the same
number in both the main program and procedure, try. The implications may seem strange at first but
they are logical.
1. The variable number in line 110 is global.
2. The variable number in procedure "test" is definitely local to the procedure.
3. The variable number in procedure "try" 'belongs' to the part of the program which was the last call
to it.
We have covered many concepts in this chapter because SuperBASIC functions and procedures are
very powerful. However you should not expect to use all these features immediately. Use procedures
and functions in simple ways at first. They can be very effective and the power is there if you need it.
PROBLEMS ON CHAPTER 15

Advertisement

Table of Contents
loading

Table of Contents