Sinclair QL Beginner's Manual page 105

Hide thumbs Also See for QL:
Table of Contents

Advertisement

CHAPTER 15 – PROCEDURES AND FUNCTIONS
In the first part of this chapter we explain the more straightforward features of SuperBASIC's
procedures and functions. We do this with very simple examples so that you can understand the
working of each feature as it is described. Though the examples are simple and contrived you will
appreciate that, once understood, the ideas can be applied in more complex situations where they
really matter.
After the first part there is a discussion which attempts to explain 'Why procedures' . If you
understand, more or less, up to that point you will be doing well and you should be able to use
procedures and functions with increasing effectiveness.
SuperBASIC first allows you to do the simpler things in simple ways and then offers you more if you
want it. Extra facilities and some technical matters are explained in the second part of this chapter but
you could omit these, certainly at a first reading, and still be in a stronger position than most users of
older types of BASIC.
VALUE PARAMETERS
You have seen in previous chapters how a value can be passed to a procedure. Here is another
example.
EXAMPLE
In "Chan's Chinese Take-Away" there are just six items on the menu.
Rice Dishes
1 prawns
2 chicken
3 special
Chan has a simple way of computing prices. He works in pence and the prices are:
for a rice dish
300 + 10 times menu number
for a sweet
12 times menu number
Thus a customer who ate special rice and an ice would pay:
300 + 10 * 3 + 12 * 4 = 378 pence
A procedure, item, accepts a menu number as a value parameter and prints the cost.
Program
100 REMark Cost of Dish
110 item 3
120 item 4
130 DEFine PROCedure item(num)
140
IF num <= 3 THEN LET price = 300 + 10*num
150
IF num >= 4 THEN LET price = 12*num
160
PRINT ! price !
170 END DEFine
Output
330 48
Sweets
4 ice
5 fritter
6 lychees

Advertisement

Table of Contents
loading

Table of Contents