Commodore VIC-20 User Manual page 358

Hide thumbs Also See for VIC-20:
Table of Contents

Advertisement

344
The VIC 20 User Guide
DEFFN
The DEF function (DEF FN) allows special purpose functions to be
defined and used within BASIC programs.
Format:
DEF FNnvar(arg)=expression
Floating point variable
nvar
identifies the function, which is sub-
sequently referenced using the name
FNnvar(data). (If nvar
has more than
five letters, a syntax error is reported. A syntax error is also reported if
nvar
is a string or integer variable.)
The function is specified by
expression,
which can be any arithmetic
expression containing any combination of numeric constants, variables, or
operators. The dummy variable name
arg
can (and usually does) appear in
expression.
arg
is the only variable in
expression
that can be specified when
FNnvar(data)
is referenced. Any other variables in expression must be
defined before
FNnvar(data)
is referenced for the first time.
FNnvar(data)
evaluates
expression
using
data
as the value for
argo
The entire DEF FN statement must appear on a single 80-character
line; however, a previously defined function can be included in
expression,
so user-defined functions of any complexity can be developed.
The function name
var
can be reused and therefore redefined by
another DEF FN statement appearing later in the same program.
The DEF FN definition statement is illegal in immediate mode. How-
ever, a user-defined function that has been defined by a DEF FN statement
in the current stored program can be referenced in an immediate mode
statement.
Example:
10 DEF FNC(R)-«*Rf2
?FNC(1)
33 IF FNC(X»60 GOTO 150
Defines afunction that calculates the circumference of
a circle. It takes a single argument R. the radius of
the circle. and returns a single numeric value. the
circumference of the circle
Prints
3.141159265
(the value of
71")
Uses the value calculated by the user-definedfunction
FNC as a branch condition. The current contents of
variable X are used when calculating the user-
defined function

Advertisement

Table of Contents
loading

Table of Contents