Commodore 2001-8 User Manual page 46

Table of Contents

Advertisement

Statement 200 uses RNDto generate an, index into C$. The random index is in the range 1 to LEN(C$) -1.
In 201 the index is used to pull N$(rank)Y$(suit) from C$ by the MID$ function.
432 through 435 removes the card from the string so that it will not be dealt again. Since the second
argument of LEFT$ cannot be zero, the R>1 test in 432 prevents an ILLEGAL QUANTITY ERROR.
440 prints each card for our program as it is pulled. 450 tests for the end of the deck and 460 allows the
user to reshuffle.
USER DEFINABLE FUNCTIONS
To this point we have covered all the functions intrinsic to BASIC. Those familiar with mathematics are
used to many more functions in that realm, especially trigonometric. While one could write code to
approximate certain functions in line it becomes very tedious and from a documentation standpoint a
simple expression might become unreadable. Fortunately ,the facility exists in PET BASIC to define
functions in terms of other functions.
A function is defined in a DEF statement:
100 INPUT B
110 INPUT C
120 DEF FN
A(V)~V/B+C
The name of the function is "FN" followed by any legal variable name. Recall that a variable is either a
letter or a letter followed by
a
letter or digit.
Thus the following are valid function names:
FNX
FNJ7
FNKO
FNR2
The most severe (imitation of user-defined functions is that they must be contained in their entirely on
one line (BD-characters). String functions cannot be defined.
The variable in parentheses follOWing the variable name is called a dummy variable. A function may be
defined to be any expression but it may have only one argument. Other variables used in the expression
are considered to be global (have the same value as in the rest of the program), and their current values
are used in the evaluation.
After the funtion definijion has been executed, a user defined function can be used as in the following
example:
130 Z
=
FNA(3)
140?Z
When the DEFFN statement is executed, a simple variable entry is made in the variable table. The first
character of the name has bit 7. the most significant bit, set to indicate it is a function name. Associated
with the name are two pointers: an address of the text where the function is stored and an address of
where the dummy variable is stored. The code to execute a function is re-entrant so that a function may
be defined in terms of other DEF FN. An out of memory error will occur in time as the available stack
space is consumed by recursion.
Figure 6.1 shows some user-defined functions which are ready to be used in PET BASIC programs.
42

Advertisement

Table of Contents
loading

This manual is also suitable for:

Pet 2001-8

Table of Contents