FN

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: FN
Abbreviation: ./.
Type: Function
Token code: ---/---
Handling routine
in BASIC ROM:
---;---
$----;----
List of all BASIC keywords


Remark: This article describes the BASIC command FN in BASIC V2 on the Commodore 64.

Type: Numeric function
General Programming-Syntax: FN <function name>(numeric expression)

The BASIC keyword FN is used in the following two constructions:

  • as part of a DEF FN definition like DEF FN <function name>(<variable>)=<expression_using_placehholder_variable>
  • calling the previously defined function like FN <function name>(<numeric expression>), described as follows ...

FN calls a function which has been previously defined in the program with the BASIC-command DEF returning a numeric value. After the definition the function can be used in any expression context. Only the first two characters are significant and following letters or digits are ignored.
The function might be executed with the command PRINT FN Y(X) even direct mode (after a break as long no CLR or NEW has been given). The space between FN is optional.

There is exactly one argument possible which has be numeric. Multiple parameters might be provided only using variables which has to be assigned before every function call (which is not very elegant).
The placeholder's argument name in the definition does not interfere with an existing variable.

If the definition via DEF is missing or not executed before the use of FN, the interpreter will report an "?UNDEF'D FUNCTION ERROR". If the numeric argument is missing or more than one is given, the interpreter will breaks with "?SYNTAX ERROR". The same happens if a variable name contains "FN", i.e. EFNA = EFNA - 2. An "?OUT OF MEMORY ERROR" can also occur if the function has been defined recursively or no space is left on the stack for some other reason.
It is possible to use different functions within one operation as in the final example given below.

Some error may be related to the function's definition: If the expression does not result in a numeric value or hasn't be called in a numeric context a "?TYPE MISMATCH ERROR" error is raised. If the function processing of the input argument calls a function with some value limitation an "?ILLEGAL QUANTITY ERROR" appears. Evaluation of the function could exceed the floating point value representation leading to an "?OVERFLOW ERROR".

Calling a FN function consumes 16 bytes on the BASIC stack. These consists of

  • float value (in variable encoding) of the argument (5 bytes)
    (in order mantissa bytes 4, 3, 2, 1 followed by the exponent byte)
  • address of the position pointing to BASIC text right after the bracket (2 bytes)
  • pointer to the value of the of the argument variable (2 bytes)
  • return address $B43A from call JSR $AD8A - Confirm Result (2 bytes)
  • return address $AD8C from call JSR $AD9E - Evaluate Expression in Text (2 bytes)
  • zero byte from $ADAB (1 byte)
  • return address $ADB1 from all JSR $AE83 Evaluate Single Term (2 bytes)

These values gets −as usual for stack operations− pushed towards lower addresses,

Examples[edit | edit source]

Execute a function in direct mode (only after the definition via DEF has been called!) ...

PRINT FN TEST(3)
PRINT FN LO10(A) 
PRINT FN X(9.873)

Usage of FN in a program:

10 A = FN DATEN1 (X) - FN JAHR2(Y)
20 H=H+1: IF FN TE3 (H+1) = 6 THEN 20
30 A=FN X(FN Y(Z))

Redefinition of the same function(!) because only 2 characters are significant, the definition of function named "F1" in line 20 wins ...

10 DEF F10TIMES(X)=X*10
20 DEF F10DIV(X)=X/10
30 PRINT F10TIMES(1)

shows the wrong result

 .1

Typical implementation to factor out the common sequence to read an address from a location (in some BASIC variants known as DEEK function)

10 DEF FN DEEK(A)=PEEK(A)+PEEK(A+1)*256
20 PRINT "BASIC PROGRAM LENGTH IN BYTES:" FN DEEK(45) - FN DEEK(43)


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT