The Computer Tutor: Simulating the DEF FN Function

The Computer Tutor heading art, Simulating the DEF FN Function
The Computer Tutor heading art, Simulating the DEF FN Function

Good morning Class! I’m wondering if the poor attendance is due to these late season snow storms or the phasing out of the TS-1000. Oh, well – today’s lesson will be on simulating the <DEF FN> or Define Function. This is a powerful programming tool that is poorly promoted and even less understood.

On many other computers, you will find this function wherever mathematical formulae need to be repeated. An example of a routine using <DEF FN> might be:

10 DFN A(X) = 17*(18.5 - X/5)
20 PRINT FNA(12)

The result of RUNning these 2 lines would be 273.7. This function, of course, could easily be written with a BASIC routine as follows:

10 INPUT X
20 GOSUB 1000
30 PRINT Y
40 STOP
1000 Y = 17*(18.5 - X/5)
1010 RETURN

For most purposes, this is easy to understand and works quite nicely. But there is a more memory efficient method. Enter the following program into your computer.

10 LET A$ = "17*(18.5 - X/5)"
20 INPUT X
30 PRINT VAL A$

Look at the advantages of this technique! We never have to access a subroutine. Enter line “40 GOTO 20” and the program recycles where we can enter different values and get the proper results each time.

The purpose of <DEF FN> is to define a function or formula where variables may keep changing but the formula that uses these variables stays intact. The TS-1000 actually has the capability to define more complex formulae than the <DEF FN> function can because formulae can be custom designed to include additional variables and be much longer.

Once the string variable (A$ in our example) has been defined, we never need encounter that program line again. Simply finding the value of that string will do the job.

A short lesson today, but one that could have important repercussions in your future programs.

Class dismissed!

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.