Home
CBM
  ASCII-X
  BASIC
    Disk Commands
    Enter RUN mode
    Program Format
    Secret Variables
    Variable Format
    Expressions
    Keywords
      (divide)
      (equal)
      (less)
      (minus)
      (more)
      (multiply)
      (plus)
      (power)
      Abs
      And
      Append
      Asc
      Atn
      Auto
      Backup
      Bank
      Begin
      Bend
      Bload
      Boot
      Box
      Bsave
      Bump
      Catalog
      Char
      Chr
      Circle
      Close
      Clr
      Cmd
      Collect
      Collision
      Color
      Concat
      Cont
      Copy
      Cos
      Data
      Dclear
      Dclose
      Dec
      Def
      Delete
      Dim
      Directory
      Dispose
      Dload
      Do
      Dopen
      Draw
      Ds
      Ds string
      Dsave
      Dverify
      El
      Else
      End
      Envelope
      Er
      Err
      Exit
      Exp
      Fast
      Fetch
      Filter
      Fn
      For
      Fre
      Get
      Get num
      Getkey
      Go
      Gosub
      Goto
      Graphic
      Gshape
      Header
      Help
      Hex
      If
      Input
      Input num
      Instr
      Int
      Joy
      Key
      Left
      Len
      Let
      List
      Load
      Locate
      Log
      Loop
      Mid
      Monitor
      Movspr
      New
      Next
      Not
      Off
      On
      Open
      Or
      Paint
      Peek
      Pen
      Pi
      Play
      Pointer
      Poke
      Pos
      Pot
      Print
      Print num
      Pudef
      Quit
      Rclr
      Rdot
      Read
      Record
      Rem
      Rename
      Renumber
      Restore
      Resume
      Return
      Rgr
      Right
      Rlum
      Rnd
      Rreg
      Rspcolor
      Rsppos
      Rsprite
      Run
      Rwindow
      Save
      Scale
      Scnclr
      Scratch
      Sgn
      Sin
      Sleep
      Slow
      Sound
      Spc
      Sprcolor
      Sprdef
      Sprite
      Sprsav
      Sqr
      Sshape
      St
      Stash
      Step
      Stop
      Str
      Swap
      Sys
      Tab
      Tan
      Tempo
      Then
      Ti
      Ti string
      To
      Trap
      Troff
      Tron
      Until
      Using
      Usr
      Val
      Verify
      Vol
      Wait
      While
      Width
      Window
      Xor
    Syntax
    Tokens
  C128
  D64plus
  Disk
  Escape Codes
  Hardware
  PCxface
  PETSCII
  Pet2asc
Futurama
IBM PC-AT
Contact
Games
Glossary
Hall of fame
Hall of shame
Miscellaneous
Privacy policy
Programming
Twisty puzzles
KeywordAbbreviationToken (hex)Version(s)Classification
DEFD{Shift+E}961.0 to 7.0Statement

 
Syntax 
DEF FN name parameter ) = expression
 
ParametersTypeLegal Value(s)Default ValueNote(s)
nameLiteral character(s)
first (required): letter
others (optional): letter or digit
 
Can not be (or include) a keyword,
but it may be the same as a reserved variable!
parameterLiteral character(s)
first (required): letter
others (optional): letter or digit
Can not be (or include) a keyword.
Can not be a reserved variable name.
expressionBASIC textany mathematical expression
Should reference parameter.
May reference other variables, including reserved variables.

 
 
Purpose 
Define a custom numeric function.

 
 
Remarks 
This statement defines a numeric "user function"; it is always required before using the function.  It may only be entered as a statement in a program; attempting to enter it as a command generates ILLEGAL DIRECT ERROR.  Calling the function (see the FN keyword) may be done in direct mode, as long as the program containing the definition has been executed, the function definition is still in program memory, and variables have not been moved or cleared (see CLR and GRAPHIC), and program lines have not been edited (see DELETE and RENUMBER).
 
The name follows the usual rules for variable naming: the first character must be a letter, the remaining characters (if any) may be either a letter or digit, only the first two characters are significant, and the name may not be (or have embedded inside of it) any BASIC keyword.  The same rules also apply to the parameter, however it has the additional restriction that it can not be the same as a reserved variable.  This restriction does not apply to name because the FN keyword, which must precede it, identifies it as a "function type" so BASIC will never be confused with "floating-point type" reserved variables.
 
The expression may be any BASIC text that calculates a numeric value.  It should reference the parameter, although it is not required.  It may also reference other variables (user defined or reserved).  The parameter must be a floating-point type variable (no suffix).  Attempting to use a string or integer type of parameter will generate SYNTAX ERROR.  If the parameter does not already exist as a floating-point variable, BASIC will create it too (following the new "user function").  Any variables referenced in the expression will not be created when defining the user function (i.e., not when using DEF), but if they do not exist when the function is called, they will be created (with a value of zero).
 
After the function has been defined, it may be used anywhere BASIC accepts a numeric value, such as in a PRINT statement, in an IF condition, in a variable assignment, or as a parameter to another command or statement.  Unlike the similar DIM (for arrays), a function may be re-defined at anytime in a program.  The most recent definition will be used when called.  Note a user function may call another user function (i.e., nested functions), however no user function may call itself, either directly or indirectly.  Doing so will generate FORMULA TOO COMPLEX ERROR.
 
This has the potential to be a really fast "subroutine", in the case where you need to perform a simple calculation.  The two main problems are it is limited to numeric expressions (you can't make a function that returns a string) and it only accepts 1 parameter.  There is no way around the first problem.  For the second problem, a dorky way to deal with the limit is to assign values of extra parameters to variables before calling the function (note this doesn't work if you need to call the function multiple times in the same expression with different extra parameters).  I'm guessing because of one or both of these reasons, the DEF doesn't show up very often in BASIC programs on the CBM series.  This doesn't mean you shouldn't use it, it just means that it isn't very popular.
 
On the Plus/4 and C128, there is a documented bug with DEF/FN (so some might claim it is a "feature").  If the program memory is moved after the function has been defined (typically (de)allocating a bitmap), attempting to call the function will generally cause a SYNTAX ERROR. (In rare cases it might return a "random" value based on the re-located program text.)  This problem exists because the pointers to functions are not updated when the program memory is moved.  To avoid the bug when using bitmaps, allocate the bitmap before defining any functions.
 
 
Examples 1:
DEF FNY(X) = X*X
?ILLEGAL DIRECT ERRROR

READY.
NEW

READY.
10 DEF FNY(X) = X*X
20 PRINT FNY(5)
30 PRINT X
RUN
 25 0
 
READY.
PRINT FNY(3)
 9

READY.
NEW

READY.
PRINT FNY(3)
?UNDEF'D FUNCTION ERROR

READY.
 
Example 2:
NEW

READY.
10 DEF FNCON(X) = 2*X+1 : REM name includes keyword "ON"
RUN
?SYNTAX ERROR IN 10

READY.
10 DEF FNC(X) = 2*X+1
20 C = 1 : X = 10 : REM has no effect on function
30 PRINT FNC(3)
40 PRINT C : PRINT X
RUN
 7 1 10

READY.
NEW

READY.
10 DEF FNF(X) = X*X+Y : REM emulate function of 2 parameters
20 Y = 100 : PRINT FNF(5)
RUN
 125

READY.
 
 
 
Compare With 
 
See Also 

© H2Obsession, 2014