home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Commodore Free 8
/
Commodore_Free_Issue_08_2007_Commodore_Computer_Club.d64
/
t.basic bible
< prev
next >
Wrap
Text File
|
2023-02-26
|
9KB
|
376 lines
uTHE BASIC BIBLE
By David Moorman
Here is an alphabetical list of BASIC
2.0 commands, functions, & operators.
Browse through them. Try them out.
Learn exactly what they do! Commands
(com) tell the computer what to do.
Functions (fun) perform magic on values
or strings in the argument (within the
parentheses), & must be used either in
a PRINT command or as the object of a
variable assignment. For example...
5 A = -1234
10 ?ABS(A)
20 V = ABS(A)
ABS(n) (fun)
Returns the positive value of the
argument, whether it is positive or
negative.
10 A = ABS(-123)
(A will contain the value of 123)
ASC(string) (fun)
Returns the ASCII value of a string
character. If the string has more than
one character, only the first
character's value is returned.
10 A = ASC("A")
(A will contain 65)
ATN(n) (fun)
Returns the arctangent of argument.
10 A = ATN(1)
(A will contain 0.785398163
arctangent of 1 in radians)
CHR$(n) (fun)
Returns a string character for the
ASCII value in the argument.
10 A$ = CHR$(65)
(A$ will contain an "A".)
CLOSE (com)
Closes logic file. See OPEN for
details.
CLR (com)
Sets all variables in a program to zero
or null (empty strings). Use at the top
of your program.
CMD lf (com)
CoMmand command. See OPEN for details
CONT (com)
CONTinue -- use in Immediate Mode to
continue running a program. Will not
work after an error or after editing
the program.
COS(n) (fun)
Returns the Cosine of the argument.
10 A = COS(1)
(A will contain 0.540302306 - cosine
of 1 in radians)
DATA (com)
Used with READ to load string or
numeric data into variables or arrays.
See READ for details.
DEF FNv(V)(com)
DEFines a FuNction. Need a complex math
function several times in a program?
Here is where you can create your own
functions. The function's name always
begins with FN, plus a single letter to
identify it, followed by a variable
within parentheses. This variable is
used in the following math as whatever
value you will later put in your
FuNction. DEFine FuNctions early in
your program.
10 DEF FNH(X)= INT(X/256)
20 DEF FNL(X)= X-FNH(X)*256
(These functions will divide a value
into High & Low byte values -- used to
address memory with some ML routines.)
30 A = 49152
40 ? FNH(A),FNL(A)
(The computer will print: 192 & 0)
DIM v [,v [,v [etc]]] (com)
DIM DIMensions space for Arrays, & is
required for arrays with more than 11
elements. You can also use DIM to tell
the program which variables you are
going to use. This puts the listed
variables at the beginning of variable
memory, so they are easier to find.
10 DIM A(12),X,Y,DT$(2,15)
END (com)
Ends the program.
EXP(n) (fun)
Returns the constant "e" (a math value)
raised to the power of the argument.
10 ? EXP(1)
(This will print 2.71828183)
FOR v TO v [step v] (com)
Begins a FOR-NEXT loop.
10 FOR X = 1 TO 10
20 ? X
30 NEXT
FRE(0) (fun)
Returns number of bytes available for
BASIC program. The argument is a dummy.
NOTE: If the value is greater than
32767, a negative value is returned
that must be added to 65535.
10 DEF FNF(x)= -(FRE(0)<0)*65535+FRE(0)
(This custom function will return free
memory as a positive value.)
20 PRINT FNF(FRE(0))
GET (com)
Gets a keystroke into the variable that
follows. A numeric variable is valid,
but the program will crash if an alpha
key is pressed. Use a string variable.
GET just grabs one keystroke, if it is
there. Use something like the following
to grab keystrokes:
10 GET Z$: IF Z$ = "" THEN 10
20 ?"YOU JUST PRESSED <" Z$ ">"
30 ?"TRY AGAIN? (Y/N)";
40 GET Z$: IF Z$ = "" THEN 40
50 ?Z$
60 IF Z$ = "Y" THEN 10
70 END
GOSUB linenumber (com)
Jumps program to a subroutine at the
given line number. Program will return
to after the GOSUB command when it
encounters a RETURN command.
GOTO linenumber (com)
Jumps program to the given line number.
IF <compare is true> THEN... When two
values or strings are compared, the
result is either -1 (for true) or 0
(for false). In an IF-THEN command, if
the value after IF is not zero
(therefore true), the code after THEN
is executed. If the value is 0, the
program "falls through" to the next
line. The code after THEN can be a line
number to jump to, or more commands.
10 IF A=B THEN 1000
20 IF B=C THEN A = B: GOTO 10
INPUT (com)
INPUT allows the user to type in
info in a program. Here are some ways
to use INPUT.
10 INPUT"WHAT IS YOUR NAME";N$
20 ?"HELLO, "N$"!"
30 ?"WHAT IS YOUR AGE (NUMBER ONLY)
40 INPUT AGE
50 ?"YOU WILL BE 100 IN"100-AGE"YEARS."
60 INPUT"PICK TWO NUMBERS, SEPARATED BY
A COMMA";A,B
70 ?"YOU PICKED:"A"AND"B"."
INPUT is an old command & can cause
many problems. An input of a string
when a value is expected will result in
an error. If a comma is included when
inputing a single variable string, it
is interpreted as two string inputs, &
everything from the comma on is lost.
Also, the user can accidentally press a
cursor key & move off the INPUT line,
screwing everything up.
Therefore, use INPUT only as you are
learning -- & always get a string
variable. You can convert a string to a
value with the VAL(string) function.
Later, when you start to use a toolbox
module or DotBASIC, you will have
alternative INPUT commands available
that work more dependably. With a
little ingenuity, you can build an
INPUT subroutine, using the GET command
to get keystrokes & put them together
in a string variable. Sounds like a
good project, eh?
INPUT#lf,v
Inputs a value or string from a disk
file. See OPEN for more information.
INT(n) (fun)
Returns the next smaller whole value of
the argument.
10 A = 123.456
20 B = -123.456
30 ? INT(A)
40 ? INT(B)
(The values printed will be 123 & -122.
We did say "next smaller!")
LEFT$(s,n) (fun)
Returns a string from string S that is
N characters long.
10 A$ = "THIS IS A TEST"
20 B$ = LEFT$(A$,4)
(B$ will contain "THIS".)
LEN(s) (fun)
Returns the length of string S.
10 A$="THIS IS A TEST"
20 ?LEN(A$)
(Prints 14.)
LET (com)
The old fashioned way to assign a
variable.
10 LET A = 12
20 LET B$ = "TEST"
This is not used anymore. Just put
the variable first, like we have done
throughout this book!
10 A = 12
20 B$ = "TEST"
LIST (com)
Lists the BASIC program in memory. Used
in Immediate Mode.
LIST > lists whole program
LIST10 > lists line 10 (if present)
LIST10- > lists line 10 & on.
LIST-10 > lists everything to &
including line 10
LIST10-20 > lists lines 10 & 20 &
everything in between.
You can slow down a LIST by holding
down the <CONTROL> key (VICE: <Tab>), &
stop it by pressing <STOP> (VICE:
<ESC>).
LOAD "filename",dv [,sa] (com)
Loads a program or file into memory
from a disk. If SA is 1, the file is
loaded to the memory location were it
was saved. LOAD is usually used from
Immediate Mode, but can be used in a
program -- with certain caveats. If one
tries to load a BASIC program from
within another program, the loaded
program must be either shorter than the
loading program, or must have a CLR as
its first command.
The loaded program will be immediately
RUN. If LOAD is used to load a binary
file, the program will jump back to the
beginning & start over. We use other
routines to load binary (data) files.
See SECRETS later in this book.
LOG(v) (fun)
Returns natural logarithm of argument.
Argument must be greater than or equal
to zero.
10 V = 7
20 PWR = LOG(V)/LOG(2)
30 ? 2^PWR
This code uses LOG to find the power
of 2 of the value 7. 2 to the PWR will
equal 7!
MID$(s,b [,l]) (fun)
Returns the section of string S
beginning at position B to the end for
the length of L.
10 A$ = "THIS IS A TEST"
20 B$ = MID$(A$,6)
30 C$ = MID$(A$,9,1)
(B$ will contain "IS A TEST". C$ will
contain "A".)
NEW (com)
Erases BASIC program memory. Careful!
NEXT (com)
See FOR-NEXT above.
ON v GOSUB ln [,ln [,ln [etc]]]
Value V determines which line number
GOSUB gosubs.
10 DIM C$(3)
20 FOR X = 1 TO 3: READ C$(X):NEXT
30 DATA "FIRE","RAM SPEED","RUN AWAY"
100 ?"CAPTIAN - WHAT SHALL WE DO?"
101 FOR X = 1 TO 3:?C$(X):NEXT
110 INPUT C$
120 Y = 0: FOR X = 1 TO 3
130 IF C$ = C$(X) THEN Y = X: X = 3
140 NEXT
150 ON Y GOSUB 1000, 2000, 3000
160 GOTO 100
1000 ?"FIRE"
1010 RETURN
2000 ?"RAM SPEED"
2010 RETURN
3000 ?"RUN AWAY"
3010 RETURN
This has got to be the shortest Star
Trek game ever written! You can take it
from here! If you have too many line
numbers to fit on two screen lines, do
something like this:
200 ON X GOSUB 1000,2000,3000
201 IF X < 4 THEN 100
202 ON X-3 GOSUB4000,5000,6000
203 IF X < 7 THEN 100
204 ON X-6 GOSUB 7000,8000,9000
205 GOTO 100
If the value is 0 or greater than the
number of available line numbers, the
program falls through. Value cannot be
negative.
ON n GOTO ln [,ln [,ln [etc]]] (com)
Just like ON-GOSUB, but with no RETURN.