Acorn Computers BBC Micro Model B w/8271 — Acorn BBC Micro Model B w/8271 User Guide Manual PDF (EN)

95 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 SUM 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 chapter, 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 after the END statement where it will not be executed unless it is called by name.