Contents / Structure within BASIC / Previous chapter / Next chapter / Index


18 Functions

Functions are in many ways similar to procedures but there is one major difference - they always calculate a result which may be a number or a string. BASIC already contains a number of functions. For example the function SQR returns the square root of a number. The square root of 16 is 4 so the statements

Y=SQR(16)

and

PRINT SQR(16)

make sense. The first example calculates the square root of 16 and places the result in Y. Compare this to a procedure - for example the one above, to draw a box. The procedure makes things happen (a box appears on the screen) but it does not produce a numeric or a string value. Functions always produce a numeric or string result.

If you have a reasonable understanding of procedures and parameters then you can probably cope with this example of a function:

10 PRINT "GIVE ME THREE NUMBERS ";

20 INPUT A,B,C

30 PRINT "THE SUM OF THE NUMBERS IS ";

40 PRINT FNSUM(A,B,C)

50 END

100 DEF FNSUM(X,Y,Z)

105 LOCAL K

110 K=X+Y+Z

120 =K

GIVE ME THREE NUMBERS ?2,4,4

THE SUN OF THE NUMBERS IS 10

Again this program is not of much use - we are using a sledge hammer to crack a nut - but we had better learn to walk before we run!

The function is defined in lines 100 to 120 and three parameters are passed to the function. A, B and C are the actual parameters and the numbers in A, B and C are passed to formal parameters X, Y and Z. For the sake of illustration a local variable K has been used. Line 110 sets K equal to the sum of X, Y and Z. Line 120 shows the way in which a function is ended. It says that the function FNSUM has the value of K.

The example above was spread out to show how a function can be constructed - it could have been compressed to

10 PRINT "GIVE ME THREE NUMBERS ";

20 INPUT A,B,C

30 PRINT "THE SUM OF THE NUMBERS IS ";

40 PRINT FNSUM(A,B,C)

50 END

100 DEF FNSUM(X,Y,Z)

120 = X+Y+Z

or even to the single line function shown below

10 PRINT "GIVE ME THREE NUMBERS ";

20 INPUT A,B,C

30 PRINT "THE SUN OF THE NUMBERS IS ";

40 PRINT FNSUM(A,B,C)

50 END

100 DEF FNSUM(X,Y,Z) = X+Y+Z

Of course we could have managed without a function at all...

10 PRINT "GIVE ME THREE NUMBERS ";

20 INPUT A,B,C

30 PRINT "THE SUM OF THE NUMBERS IS ";

40 PRINT A+B+C

50 END

...and clearly that would have been the right thing to do in this case. However as soon as your programs reach 40 or 50 lines you should be using procedures extensively and functions occasionally.

As mentioned at the start of this section, functions can be used to calculate a numeric or a string result. The function which follows returns the middle letter of a string. The string is passed as a parameter

100 DEF FNMID(A$)

110 LOCAL L

120 L=LEN(A$)

140 =MID$(A$,L/2,1)

Again, the function is terminated by a statement starting with an equal sign. To use the above function type in the following additional lines

10 INPUT Z$

20 PRINT FNMID(Z$)

30 END

Notice that the function is placed beyond the END statement where it will not be executed except by being called by name.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001