Commodore VIC-20 Programmer's Reference Manual page 32

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

Advertisement

When a long mathematical formula is used several times in
different lines of a program, program memory and typing time can
be saved by using a defined function for the formula. The function is
then used throughout the program in place of the lengthy formula.
The name of the function will be the letters FN and any variable
name you choose, one or two letters long. The DEF FN statement
must be executed at least once for the program to use it, so this
statement is normally placed at the beginning of the program.
The function name is followed by a variable name inside
parentheses. Next comes an equal sign, and then the formula.
Here's an example of a simple formula definition, with an
example of its use in the program.
EXAMPLE 1:
10 DEF FNA(X) = 7 * X
20 PRINT FNA(1}
30 PRINT FNA(3)
The result of line 20 is 7, and the result in line 30 is 21.
EXAMPLE 2:
10 DEF FNA(X) = INT(RND(1)*6) + 1
20 PRINT FNA(10)
When the function in this example is used in the program, the
value of the number in parentheses in line 20 doesn't have any
effect on the result. This is because in line 10 the variable (X) in the
parentheses doesn't appear in the formula on the right.
The next example does use the variable name in the formula.
EXAMPLE 3:
10 DEF FNA(X) = INT(RND(1)-X} + 1
20 PRINT FNA(10)
In this case, the number in the parentheses in line 20 does affect
the result. The number in the parentheses in line 20 is the largest
random number that will be picked.
The result of a defined formula must always be a number; there
are no defined functions for string variables.
I
16
]

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents