Sinclair QL Beginner's Manual page 108

Hide thumbs Also See for QL:
Table of Contents

Advertisement

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 write 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) + price(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.
WHY PROCEDURES?
The ultimate concept of a procedure is that it should be a 'black box' which receives specific
information from 'outside' and performs certain operations which may include sending specific
information back to the 'outside: The 'outside' may be the main program or another procedure.
The term 'black box' implies that its internal workings are not important: you only think about what
goes in and what comes out. If for example, a procedure uses a variable, count and changes its
value, that might affect a variable of the same name in the main program. Think of a mail order
company You send them an order and cash: they send you goods. Information is sent to a procedure
and it sends back action and/or new information.

Advertisement

Table of Contents
loading

Table of Contents