Exploring Def FN and FN

See all articles from Update July 1991
Author(s): Bill Jones
Publication: Update Magazine
Pub Details: Update July 1991
Date: July 1991
Pages: 23-24
BASIC Terms: DEF FN, FN

For All Computers

There are many amongst us who could broaden our MATH by use of the DEF FN and FN functions. DEF FN gives us 52 FORMULA storage slots to be exercised by a simple use of the FN command. Most of us have to really search the TS-2068 keyboard to find the appropriate Key. (under the 1 and 2 keys). For the Ql and Z88 we simply type the DEF FN and FN commands.

There are 26 possible numerical expressions of the DEF FN command, one for each letter of the alphabet. There are 26 more for literal expressions. In each case we can think of DEF FN as “holding a formula”. And, we can think of FN as a command to execute the formula. A typical numeric formula is given as: 200 DEF FN a()=200/PI. To execute the formula: PRINT FN a(). OR <LET x=FN a(). You can even <LET a=FN a()> as the FN letter is not a variable. The formula MUST be in a LINE of PROGRAMMING, because each usage of FN causes a search of program lines for the formula to execute.

A LITTLE PROGRAM DOES a LOT of THINGS: The following little program demonstrates several ways of using DEF FN and FN. Note at line #100 that we assign the FN values to variables to use within the DEF FN formula. This works because none of the variables are required in line numbers “lower than 100”. The reason for using variables is because we cannot use FN “within a DEF FN formula”. Example: 200 DEF FN a=DEF FN b()*10. That wouldn’t work. So, to use DEF FN b() in the formula we must first assign a variable to carry the value. Example: LET y=FN b(). Then: 200 DEF FN a()=y*10. There isn’t any other restrictions. In fact, DEF FN is somewhat similiar to the QL’s Super basic “Procedures”, except less restrictive.

Now to explain the little Demo Program. There are 7 examples of DEF FN Formula or expressions. These are in lines 20, 40, 45, 50, 55, 127, and 128. The first 5 are explained with REMarks in the program line. Incidentally, the program lines could be any number high or low. And the lines can contain both the DEF FN formula and programming to be executed.

INPUTS begin at line #40 for the needed variable assignments for the DEF FN Formula to work. Then at line #105 we demonstrate that a new variable (width) can be input and the other new parameters of the formula can be gotten via the FN command.

Line numbers 30, 40, and 125, go together to demonstrate several unique ways of using the DEF FN function. At line #30 we initialize a String Variable. Then at line #40 we use DEF FN a$() to slice the string’s first five (literal) numbers. Then at line 125 we use VAL to convert the literal string into a number, and multiply the number by 2.

Line #127 assigns the DEF FN d() formula in an unusual way, using Boolean Logic. Then the logic value is printed.

Line #128 uses the Boolean logic of line 127, and another Boolean evaluation to assign the DEF FN c$() formula. Then line 128’s FN command gets the solution and prints it (“Hello or Goodbye”). Obviously, looking at the Boolean examples, the power of DEF FN is expanded tremendously over simple mathematics formula resolution. Boolean logic can tie in several differential equations and select the proper one for execution, depending upon a variety of possible circumstances.

For example, line #127’s Boolean logic could make DEF FN d() equal as many as 20 or more different values, just by adding more logic to the bracketed evaluations. Then line #128’s Boolean evaluators could be as many conditons as line 127’s formula could produce. The only real limitation of the number of Boolean formula would be the capacity of the screen to key in the program line’s logic. I often use as many as 12 Boolean conditions in one program line formula. I’m not trying to dazzle, but to say that there is no maximum limit to the way that DEF FN can solve both pure math formula and LOGIC itself.

Lets look at a long but simple formula assignment for DEF FN a(). The senario is, a menu is used to elect the type of math formula to solve: 1. Area of Rect. 2. Width of Rect. 3. Length of Rect. 4. Area of Circle. 5. Circumference of circle. (we could add a dozen more). Variable Z is assigned the number touched after the menu. ie, z=1 if item #1 is elected.

The DEF FN formula would be: DEF FN a()=(l*w AND z=1)+(a/l AND z=2)+(a/w AND z=3)+(PI *r^2 AND z=4)+(PI *d AND z=5). Then <PRINT FN a() > would produce the solution for any of the five math problems. Of course, the needed inputs of variables would preceed the solve line. SO, if a dozen or more math problems can be solved by one DEF FN a() formula, the limitation of 52 DEF FN’s does not seem to be a limit at all.

I believe that the little program demo will give enough examples to whet anyone’s appetite to make use of the DEF FN and FN functions. There isn’t much text written about these functions that are present in most computers. The manuals wont even give an inkling of the versatility that the functions have, nor how to use them. Experimentation is the only way to develop one’s prowess. If Einstein had had DEF FN he would have produced a formua for “anti-gravity. Try your luck and you may do that. Have fun! -BJ_

  5 REM ** Applicable to ALL TS-2068, Z88, and QL
6 REM
7 REM Adjust syntax for QL / Z88
10 REM "Demo of DEF FN and FN" Type RUN
15 REM
20 DEF FN a()=PI*r^2: REM Triangle Area
30 LET k$="1234567890"
40 DEF FN a$()=k$(1 TO 5): REM Slices k$
45 DEF FN b()=l*w: REM Area of Rectangle
50 DEF FN w()=x/l: REM Width of Rectangle
55 DEF FN l()=x/w: REM Length of Rectangle
60 INPUT "Input radius ";r: PRINT "The Circle area is ";FN a()
65 INPUT "Enter Rect Width/Length";w,l: PRINT "The Rect Area is ";FN b()
100 LET x=FN b(): LET y=FN w(): LET z=FN l()
105 INPUT "Enter New Rect Width ";w: PRINT "The New Length is ";FN l()
125 PRINT "FN a$()*2=";VAL FN a$()*2: REM converts slice to number, times 2
127 DEF FN d()=(0 AND w<20)+(1 AND w>19): PRINT "FN d() Boolean logic=";FN d()
128 DEF FN c$()=("Hello" AND y<1)+("Good Bye" AND y>0): PRINT FN c$()

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