--- title: "Programming Tips (all computers): Exploring Def FN and FN" type: "article" slug: "programming-tips-all-computers-exploring-def-fn-and-fn" url: "http://localhost/article/programming-tips-all-computers-exploring-def-fn-and-fn/" markdown_url: "http://localhost/article/programming-tips-all-computers-exploring-def-fn-and-fn.md" published_at: "2020-10-27T17:14:19+00:00" modified_at: "2026-11-08T09:53:08+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "How to set up and use user-defined functions." post_tag: - name: "TS 2068" slug: "ts2068" taxonomy: "post_tag" url: "http://localhost/tag/ts2068/" - name: "Tutorial" slug: "tutorial" taxonomy: "post_tag" url: "http://localhost/tag/tutorial/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" basic: - name: "DEF FN" slug: "def-fn" taxonomy: "basic" url: "http://localhost/basic/def-fn/" - name: "FN" slug: "fn" taxonomy: "basic" url: "http://localhost/basic/fn/" model: - name: "Timex/Sinclair 2068" slug: "ts-2068" taxonomy: "model" url: "http://localhost/model/ts-2068/" indiv: - name: "Bill Jones" slug: "bill-jones" taxonomy: "indiv" url: "http://localhost/indiv/bill-jones/" publication: "Update Magazine" publication_r: id: 10283 title: "Update Magazine" type: "periodical" url: "http://localhost/periodical/update-magazine/" authors: "Bill Jones" issues_articles: - id: 26495 title: "Update January 1991" type: "issue" url: "http://localhost/issue/ts-2068-up-date-january-1991/" pages: "43-45" pubdate: "January 1991" archive_link: false volumeissue: "vn" --- # Programming Tips (all computers): Exploring Def FN and FN ### Exploring DEF FN and FN The little used DEF FN and FN functions are very Powerful and quite easy to use. Many have ask for an article explaining how to use DEF FN, so here it is. is used in a line of programming. The program line DEFines a Function. A very simple one (of no value) would be: ``` 9000 DEF FN a()=10 ``` This is similiar to the way we would assign a value to the variable a. But the formula in line 9000 DOES NOT assign a variable. Its “a()” is a Defined Function and NOT a variable. is used to EXECUTE THE DEF FN Function. EXAMPLE: To execute the Function in line 9000: ``` 100 PRINT FN a() ``` (And 10 would be printed to screen). Now lets use a trio of DEF FN statement lines to use for computing the simple formula for the area of a rectangle. ``` 9000 DEF FN a()=L*W 9002 DEF FN L()=a/W 9004 DEF FN W()=a/L ``` Here we have three formula set up to compute Area, Length, and Width. Now we will EXECUTE the formula with the FN function. ``` 100 LET l=0:LET w=0:LET a=0 102 INPUT "<1> Find Area. <2> Find Length <3> Find Width";x 104 IF x=1 THEN INPUT "ENTER L";l:INPUT ENTER w";w 106 IF x=2 THEN INPUT "ENTER A";a:INPUT ENTER W";w 108 IF x=3 THEN INPUT "ENTER Area";a: INPUT "ENTER Length";l 110 IF x=1 THEN PRINT FN a() 112 IF x=2 THEN PRINT FN l() 114 IF x=3 THEN PRINT FN w() 116 STOP ``` ### DEF FN used for Literal Data ADD a line: 9005 DEF FN y$()=a$ Now we will use the DIRECT MODE. Type Type The last little exercise tells us that a DEF FN statement can be much broader than we have thought before. It is not limited to MATH Functions. IN FACT, ANY FUNCTION that can be expressed in proper BASIC SYNTAX can be DEFined in a DEF FN statement! CAN WE EXPAND DEF FN to exercise Machine Code Utilities? Why not, since BASIC can exercise a MC program? If a MC program produces NUMERIC value via then we can make the DEF FN pick up the value: 9010 DEF FN a()=USR x>. Or, (9020 DEF FN a()=(USR x)+(USR y) >. But this is getting beyond the intent of this treatsie. Already we have learned quite a bit about DEF FN. 1. DEF FN can define a math formula or any computer function to be executed. 2. The DEF FN “variable” has no relationship to a “computer variable”. 3. The DEF FN assignment must be in a PROGRAM LINE 4. Once the DEF FN assignment is in a program line “It is just there”, and no GO TO or GO SUB is needed to execute the Function. 5. The “FN” function executes the DEF FN statement. 6. The DEF FN Function is DEFined to the right of the “=” sign and can be ANY Computer Function. 7. The Function can be ANYTHING that the Computer is capable of doing (If in proper Syntax). Now to really tie down the DEF FN and FN functions lets key in a couple of demonstration programs. Believe me, by doing so you’ll never again ask about DEF FN and FN. AND a large new capability will be discovered. Experimentation will broaden the discovery. Have Fun– [Bill Jones](http://localhost/indiv/bill-jones/) ### Choo-Chooing Along With DEF FN A Train of Examples ``` 300 CLS : PRINT "Investigating DEF FN"'' 302 GO SUB 480: PRINT "--Area of a Circle" 304 INPUT "Enter Radius";r: PRINT "The Area is ";FN a(): PRINT "ENTER": PAUSE 0 306 CLS : GO SUB 480:: PRINT "DEF FN to Find Systems Vars"'' 308 PRINT "Find VARS"'"VARS=";FN v(): GO SUB 480: PRINT "ENT": PAUSE 0 310 GO SUB 480: PRINT "Find PROG": PRINT "PROG is on the Log at ";FN p(): PRINT "ENT": PAUSE 0 312 GO SUB 480: PRINT "Find NXTLIN"'"NXTLIN=";FN n(): PRINT "ENT": PAUSE 0 314 GO SUB 480: PRINT "Find WORKSP"'"WORKSP=";FN w(): PRINT "ENT": PAUSE 0 316 GO SUB 480: PRINT "Find STKBOT"'"STKBOT=";FN s(): PRINT "ENT": PAUSE 0 318 GO SUB 480: PRINT "Find RAMTOP"'"RAMTOP=";FN r(): PRINT "ENT": PAUSE 0 320 GO SUB 480: PRINT "Find P-RAMT"'"P-RAMT=";FN m(): PRINT "ENT": PAUSE 0 322 CLS : GO SUB 480: LET x=4e4: PRINT "Doing Square Roots"'': FOR n=1 TO x: INPUT "ENTER A Number-x To Quit";a: IF a=x THEN GO TO 340 324 PRINT "The Square Root of ";a;" is ";FN z(): NEXT n 340 CLS : PRINT INVERSE 1;"Seven Inter-dependant DEF FN Formulae To Investigate "; INVERSE 0''TAB 5;"How Much Water?"'': PRINT "Given: Last Night a big Thunder-Storm rained over a 25 Square Mile Area."''"How many Inches do you suppose Fell?"'': GO SUB 480: INPUT "Input Inches";a 341 CLS : PRINT "If ";a;" Inches over 25 Sq Mi :"'' 342 PRINT "There would be "; INVERSE 1;FN l(); INVERSE 0'"Cubic Inches of Water."'': GO SUB 480 343 PRINT "This would be "; INVERSE 1;FN u(); INVERSE 0;" Cu Ft."; INVERSE 0'': GO SUB 480 344 PRINT "This is "; INVERSE 1;FN g(); INVERSE 0;" Gallons."'': GO SUB 480 345 PRINT "The Weight is "; INVERSE 1;FN o(); INVERSE 0'"Pounds."'': GO SUB 480 346 PRINT "The Water would Weigh "; INVERSE 1;FN t()'" Tons."' INVERSE 0: GO SUB 480 347 PRINT "A Choo-Choo Train of "; INVERSE 1;INT (FN c()); INVERSE 0'"Tank Cars would be needed to haul the Water."'': GO SUB 480 348 PRINT "The Train would be "; INVERSE 1;INT (FN y()+.5);" Miles"' INVERSE 0;"in Length"'': GO SUB 480 350 PRINT '"At 100 Cars Per Engine, we wouldneed "; INVERSE 1;FN h(); INVERSE 0;" Locomotives."'': GO SUB 480 352 PRINT "Engines extend the Train Length to "; INVERSE 1;FN j(); INVERSE 0;" Miles long.": GO SUB 480 354 PRINT "ENTER": PAUSE 0: GO TO 340 480 FOR n=1 TO 3: BEEP .15,n*14: NEXT n: RETURN 500 DEF FN a()=(PI*r^2) 502 DEF FN v()=(PEEK 23627+256*PEEK 23628) 504 DEF FN p()=(PEEK 23635+256*PEEK 23636) 506 DEF FN n()=(PEEK 23637+256*PEEK 23638) 508 DEF FN e()=(PEEK 23641+256*PEEK 23642) 510 DEF FN w()=(PEEK 23649+256*PEEK 23650) 512 DEF FN s()=(PEEK 23651+256*PEEK 23652) 514 DEF FN r()=(PEEK 23730+256*PEEK 23731) 516 DEF FN m()=(PEEK 23732+256*PEEK 23733) 518 DEF FN z()=(SQR a) 520 DEF FN l()=((5280*12)^2)*a*25: REM Cubic Inches 521 DEF FN u()=FN l()/1728: REM Cubic Feet 522 DEF FN g()=(FN l()/231): REM Gallons 524 DEF FN o()=FN g()*8: REM Weight Pounds 526 DEF FN t()=(FN o()/2000): REM Weight Tons 528 DEF FN c()=(FN o()/90000): REM Nbr Rail Cars 90000 lb Gwt 530 DEF FN y()=(FN c()*80/5280): REM Train length 80 Feet per Car 532 DEF FN h()=INT (FN c()/100+.5) 534 DEF FN j()=INT (FN h()*80/5280+FN y()+.5) ``` Note: The Program beginning at line #340 uses 9 DEF FN Formulae. These begin at line #520 and are set up in a sequence so that each formula AFTER line 520 depends upon the calculation of earlier DEF FN functions. So, for the last calculation in line #352, the formula within lines 520, 521, 522, 524, 526, 528, 530, and 532, are all executed to find “FN j()” in line 352. This difficult math problem was given in a test to a 12th grade high school class, accompanied by a table of values needed to solve the problem. Of three classes of 30 students each, none solved the problem within a 10 mile error allowance. The TS-2068 solves the problem in about 2 seconds, most of which is used for display. -BJ_ ``` 50 REM ** Differential 51 REM Equations 52 REM Using DEF FN 53 REM 54 REM ** Area of a Circle 55 REM (for simplicity) 56 REM 100 LET a=0: LET r=0 102 INPUT "<1> Find Area"'"<2> Find Radius";x 104 IF x=1 THEN INPUT "Enter Radius ";r 106 IF x=2 THEN INPUT "Enter Area ";a 108 IF x=2 THEN PRINT "The Radius is ";FN r() 110 IF x=1 THEN PRINT "The Area is ";FN a() 120 GO TO 100 9000 DEF FN a()=PI*r^2 9002 DEF FN r()=SQR (a/PI) 9003 REM ----------------- 9004 REM * After Breaking OUT of the above, do some in the Direct Mode. TRY: 9005 REM ----------------- 9008 REM ** Note that A DIRECT Command works if there is a valid Formula in a Line Statement. If NOT then a Report Code would be given. 9010 REM ----------------- 9016 REM * Also prove that vars a and r are not disturbed by the DEF FN assignments of DEF FN a() and DEF FN r(). and and 9018 REM ----------------- 9020 REM * Now we are equipped to construct a better View Calc program, eh? 9022>REM ----------------- 9024 REM * Add more lines with your favorite Math Formulae to Compute the National Debt on a daily basis. (In Dollars, Yen, and Schekels) ```