BASIC09 Command Reference / 11
DATE$
Provide date and time
Syntax: DATES
Function:
Returns the date and time. The OS-9 internal date
is kept in the format:
yearlmonthl day hour: minutes:seconds
If your OS-9 Startup file contains the SETIME command, the
system asks you to enter the date and time whenever it boots.
If it does not contain the SETIME command, the date and
time start from
86/09/01:00:00:00.
You can use the normal string functions to access the data
contained in DATES, but you cannot use functions or operations that attempt to change or append to its values. To reset
the date or time or both, use the SHELL command, such as:
SHELL "SETIME"
Parameters:
None
Examples:
PRINT DATES
Sample Program:
This program is essentially the same as the sample program for
the DATA statement, except that it gets the day, month, and
year from DATE$.
PROCEDURE date
ODIM X,DAY,MONTH,YEAR,CALC:INTEGER
ODIM ANUM,HNUM,CNUM,DNUM,ENUM,FNUM,GNUM,HNUM,INUM:INTEGER
ODIM WEEKDAY(7):STRINGf91
[]MONTH=VAL(MID$(DATE$,4,2)) (* get month from DATES.
[]DAY-VAL(MID:(DATE=,7,2)) (* get day from DATE.
DYEARzVAL("19"+LEFTS(DATES,2)) (* get year from DATES.
[]FOR X=1 TO 7
DREAD WEEKDAY(X)
11-27
BASIC09 Reference
ONEXT X
pANUM= I NT(, 6+1 /MONTH)
pHNUM=YEAR-ANUM
pCNUM-MONTH+12*ANUM
pDNUM=HNUM/188
DENUM=INT(DNUM/4)
OFNUM=INT(DNUM)
pGNUMzINT(S*HNUM/4)
OHNUM=INT(13*(CNUM+1)/S)
pINUMzHNUM+GNUM-FNUM+ENUM+DAY-1
pINUM=INUM-7*INT(INUM/7)+1
pPR I'K
pPRINT "Today is "; WEEKDAY(INUM)
ODATA "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
ODATA "Saturday"
OEND
11-28
BASIC09 Command Reference l 11
DEG
Return trigonometric calculations in
degrees
Syntax: DE G
Function:
Causes a procedure to calculate trigonometric values in degrees. If you do not include the DEG statement, procedures produce radian values.
Parameters:
None
Examples:
DEG
Sample Program:
This procedure calculates the sine, cosine, and tangent for a
value you enter. Because it uses the DEG statement, it displays
the results in degrees.
PROCEDURE degcalc
DDIM NUM:REAL
DDEG
DINPUT "Enter a number
DPRINT
DPRINT "Number", "SINE","COSINE","TAN"
",NUM
DPRINT NUM,SINCNUM),COSCNUM),TANCNUM)
DPR I NT
REND
-------------
11-29
BASIC09 Reference
DELETE
Erase a disk ale
tax: DELETE "pa thname"-
Function:
DELETE removes a file from disk storage and
releases the portion of the disk on which it resides. When you
DELETE a file, it is permanently lost.
Parameters:
pathname The complete pathlist to the file you want to
delete, including the drive and one or more
directories, if appropriate. You must surround
the pathlist with quotation marks.
Examples:
DELETE "myf i 1 e"
DELETE "/D1/ACCOUNTS/receivable5"
Sample Program:
This procedure creates a file named Samplefile, writes data to
the file, then closes it. It then lists the file before deleting it.
PROCEDURE close
ODIM PATH:HYTE
OCREATE OPATH,"samplefile":WRITE (* create a file,
OWRITE OPATH,"This file is for testing purposes only."
OWRITE OPATH,"It will be destroyed when this procedure ends."
OCLOSE #PATH (* close the file.
OSHELL "LIST 5amplefile"
ODELETE "5amplefile"
OEND
11-30
BASIC09 Command Reference / 11
DIM
Assi variable storage
Syntax: DIM variable[,...] [:
type)[, variable][,...][. type)[...)
Function: Assigns storage space and declares types for variables, arrays, or complex data structures.
Parameters:
variable
A simple variable, an array structure, or a
complex data structure.
type BYTE, INTEGER, REAL, BOOLEAN,
STRING, or user defined.
Notes:
· You declare simple arrays with DIM by using the variable
name, without a subscript. If you do not explicitly declare
variables, the system makes them type real unless they are
followed by a dollar sign ($). The system dimensions variables ending with a dollar sign ($) as strings, with a length
of 32 bytes. You must declare types of all other simple variables as to type.
· You can declare several variables of the same type by separating them with commas. To separate variables of different types, follow each type group with a colon, the type
name, and then a semicolon.
· Define a maximum length for a string variable by enclosing
the length in brackets following the type, like this:
DIM name:5tring[251
11-31
BASIC09 Reference
If you do not define a maximum length, BASIC09 uses a
default length of 32 characters. You can declare a shorter
length or a longer length, up to the capacity of BASIC09's
memory. If you try to extend a string beyond its declared
length, or beyond the default length, the system ignores all
extra characters. Thus the following:
DIM name:5trin9[101
name = "Abbernathin5ky"
produces the string:
Abbernathi
Arrays can have one, two, or three dimensions. The DIM
format for dimensioned arrays is the same as for simple
variables, except that you must follow each array name
with a subscript, enclosed in parentheses, to indicate its
size. The maximum array size is 32767.
Arrays can be either of the standard BASIC09 type or of a
user-defined type. For information on creating your own
types for simple variables, arrays, and complex data structures, see TYPE.
Examples:
DIM logica1:H00LEAN
DIM a,b,c:INTEGER
DIM name,addre55,zip:STRING
DIM name:STRINGL25l; addre55:STRINGt30J;
zip: INTEGER
DIM not,not,no3:REAL;no4,noS,no6:INTEGER;
no7:BYTE
11-32
BASIC09 Command Reference / 11
Sample Program:
This procedure randomly selects letters and vowels to create sixletter words that might look like alien names. It first DIMS nine
string variables to contain the letters selected for each name. It
DIMS two integer variables to provide a loop counter and to store
the number of names you request.
When asked, type the number of names you want to have the
procedure generate.
PROCEDURE alien
ODIM H,HEGIN,F,FINISH:STRING
ODIM VOWELS,VOWEL1,VOWEL2:STRING
ODIM MID1,MID2:STRING
ODIM T,RESPONSE:INTEGER
OVOWELS="aeiouy"
OINPUT "How many alien names do you want to
See?...",RESPONSE
OHEGIN="AHCDFGHJKLMNPRSTVWXZ"
OFINISH="ehlmnprstvwyz"
OFOR T=1 TO RESPONSE
OH=MID$CHEGIN,RNDC19)+1 ,1 )
OF=MID$CFINISH,RNDC12)+1 ,1 )
OMID1=CHR$CRNDC25)+97)
OMID2=CHR$CRNDC25)+97)
OVOWEL1 =MID$CVOWELS,RNDC5)+1 , 1 )
OVOWEL2=MID$CVOWELS,RNDCS)+1 ,1 )
OPRINT H; VOWEL1 ; MID1 ; MID2; VOWEL2; F,
ONEXT T
OPRINT
REND
11-33
BASIC09 Reference
DO
Execute procedure lines in a loop
Syntax: WHILE expression DO
proclines
ENDWHILE
Function: Establishes a loop that executes the procedure lines
between DO and ENDWHILE as long as the result of the
expression following WHILE is true. Because the loop is
tested at the top, the lines within the loop are never executed
unless expression is true.
Parameters:
expression A Boolean expression (produces a result of
True or False).
proclines
Are program lines to execute if the expression
is true.
See WHILE/DO/ENDWHILE for more information.
11-34
BASIC09 Command Reference l 11
ELSE
Execute alternate action
Syntax: IF condition THEN
action
ELSE
secondary action
ENDIF
Function: ELSE provides access to a secondary action within
an IF/THEN test. When the condition tested by IF is not
true, BASIC09 executes the
secondary action
preceded by
ELSE.
Parameters:
condition
action
secondary
action
A Boolean expression (produces a result of
True or False).
A line number to which the procedure is to
transfer execution, or a program statement. If
action is a line number, do not include the
ENDIF statement in the IF test.
One or more program statements.
For more information, see IF/THEN/ELSE
11-35
BASIC09 Reference
END
Terminate a procedure
Syntax:
END ["text"]
Function:
Ends procedure execution and returns to the calling
procedure, or to the highest level procedure. If you provide
output text for END, it functions in the same manner as
PRINT. You can use END several times in the same procedure. END is not required as the last statement in a
procedure.
Parameters:
text A literal string or a string-type variable.
Examples:
END "Program Terminated"
LAST="Session over"
END LAST
Sample Program:
This procedure calculates a loan's term, using END to terminate routines.
PROCEDURE loaner
pDIM YOUPAY,PRINCIPLE,INTEREST,NUMPAY,YEARS,
MONTHS:REAL
ODIM RESPONSE:STRINGL1J
pREPEAT
OPRINT
OPRINT USING "S45^","Loan Terms"
pPRINT -
pINPUT " Amount of Regular Payments ",YOUPAY
OINPUT " Enter the Principle ",PRINCIPLE
DINPUT " Enter the Annual Interest Rate ",
INTEREST
pINPUT " Enter the Number of Payments
Yearly",NUMPAY
11-3s
BASIC09 Command Reference l 11
OYEARS=-CLOGC1-PRINCIPLE*CINTEREST/100)/
CNUMPAY*YOUPAY))/CLOGC1+INTEREST/100/NUMPAY)*
NUMPAY))
OMONTH=INTCYEARS*12+.S)
pYEARS= I NTCMONTH/ 1 2 )
OMONTH=MONTH-YEARS*12
pPRINT " The Term of Your Loan is "; YEARS; "
years and "; MONTH; " months."
OINPUT "Calculate another or Quit CC/Q)?...",
RESPONSE
DUNTIL RESPONSE<>"C" AND RESPONSE<),"c"
OEND "Goodbye-I hope I helped you."
11-37
BASIC09 Reference
END
E XIT
Leave loop if a condition is True
Syntax: E XITIF condition THEN
proclines
ENDEXIT
Function: ENDEXIT terminates an EXITIF test. You always
use EXITIF/THEN/ENDEXIT inside a procedure loop. If the
Boolean expression tested by EXITIF is true, BASIC09 executes the program statements between THEN and ENDEXIT
and then transfers program operation outside the loop. If the
condition tested by EXITIF is not true, loop execution continues at the statement following ENDEXIT.
Parameters:
condition
A comparison operation that returns either
True or False, such as A = B, A<B, or
A=B=C.
proclines One or more statements to perform if the Boo
lean expression tested by EXITIF is True.
For more information, see EXITIF/THEN/ENDEXIT
11-38
BASIC09 Command Reference / 11
ENDIF
Close IF statement
Syntax:
IF condition THEN
action
[ELSE
secondary action]
ENDIF
Function:
ENDIF terminates an IF/THEN condition test. If
the condition tested by IF is true, BASIC09 executes the
statements between THEN and ENDIF. If the condition tested
by IF
is not
true, BASIC09 transfers execution to the procedure line following ENDIF or (optionally) executes the statements following ELSE.
Parameters:
,^ condition A
Boolean expression (produces a result of
True or False).
action A
line number to which the procedure is to
transfer execution.
Action
can also be a pro
gram statement. If action is a line number, do
not include the ENDIF statement in the IF
test.
secondary A
program statement.
action
For more information, see IF/THEN/ELSE/ENDIF.
11-39
BASIC09 Reference
ENDLOOP
Close LOOP statement
Syntax: LOOP
statements)
ENDLOOP
Function:
ENDLOOP terminates a procedure loop established
by the LOOP command. BASIC09 endlessly executes all procedure statements between LOOP and ENDLOOP repeatedly
unless a condition test within the loop (such as EXITIF/
THEN/ENDEXIT, or IF/THEN) transfers execution outside of
the loop.
Parameters:
staternent(s)
One or more procedure lines that execute
within the loop.
For more information, see LOOP/ENDLOOP.
11-40
BASIC09 Command Reference l 11
ENDWHILE
Close WHILE statement
Syntax: WHILE condition DO
proclines
ENDWHILE
Function:
Forms the bottom of a WHILE loop. WHILE causes
the procedure lines between DO and ENDWHILE to execute
as long as the result of the expression following WHILE is
true. Because the loop is tested at the top, the lines within
the loop are never executed unless the expression is true.
Parameters:
condition
A Boolean expression (produces results of True
or False).
proclines
Are program lines to execute if the expression
is true.
For more information, see WHILE/DO/ENDWHILE.
11-41
BASIC09 Reference
EOF
Test for end-of-file
Syntax:
E OF (path)
Function:
Tests for the end of a disk file. The function returns
a value of True when it encounters an end-of-file; otherwise, it
returns False. Use EOF with a READ or GET statement.
Parameters:
path The number of the path you are accessing.
BASIC 09 automatically stores a path number
into the variable you specify during a
CREATE or OPEN operation.
Examples:
IF EOF(xPATH) THEN
CLOSE #PATH
ENDIF
Sample Program:
This procedure redirects a listing of the current directory into a
file named Dirfile. It then lists Dirfile to the screen. EOF tells
the WHILE/ENDWHILE loop when the READ operation reaches
the end of the file.
PROCEDURE readfile
ODIM A:STRINGI80J
ODIM PATH:BYTE
OSHELL "DIR > dirf ile"
OOPEN #PATH,"dirfile":READ
OWHILE NOT EOF(#PATH) DO
DREAD *PATH,A
OPRINT A
OENDWHILE
OCLOSE #PATH
[]END
11-42
BASIC09 Command Reference / 11
ERR
Return error code
Syntax: ERR
Function:
Returns the error code of the most recent error.
BASIC09 automatically sets the ERR code to zero after you
reference it. ERR is only useful when used in conjunction with
BASIC09's ON ERROR error trapping functions.
See Appendix A for a list of all BASIC09 error codes.
Parameters:
None
Examples:
ERRNUM = ERR
IF ERRNUM = 218 THEN
PRINT "File already exists. Please use another
`~ filename."
ENDIF
Sample Program:
This procedure displays the contents of a file you select. If the
file doesn't exist (Error 216, Pathname not found), the procedure
uses ERR to tell you. If an error other than Error 216 occurs,
the procedure displays
I can't handle error x x,
where xx is
the code of the error.
11-43'
BASIC09 Reference
PROCEDURE readfile
pDIM READFILE:STRING; A:STRINGI80J; PATH:BYTE
100INPUT "Type the pathli5t of the file to read...",READFILE
pON ERROR GOTO 100 (* if an error occurs, skip to line 100.
pOPEN #PATH,READFILE:READ
OWHILE EOF(#PATH)c)TRUE DO
pREAD #PATH,A
pPR I NT A
DENDWHILE
OCLOSE #PATH
pEND
100DERRNUM-ERR (* store the error code in ERRNUM.
pIF ERRNUM-216 THEN (* if file doesn't exist say
5o.
OPRINT "I can't find the file ...Please try again."
OON ERROR
pGOTO 10
OENDIF
pPRINT "Sorry, I can't handle error number "; ERRNUM ( other error.
OCLOSE #PATH
pEND
11-44
BASIC09 Commands Reference
/ 11
ERROR
Simulate an error
Syntax: ERROR code
Function:
Simulates the error specified by
code. You would
mainly use this command to test ON ERROR GOTO routines.
When BASIC09 encounters an ERROR statement, it proceeds
as if the error corresponding to the specified code has
occurred. Refer to Appendix A for a listing of error codes and
their meanings.
Parameters:
code
The code of the error you want to simulate.
Examples:
ERROR 207
ERRNUM = ERR
IF ERRNUM = 207 THEN
PRINT "Memory is full. The current data is being
saved to disk."
ENDIF
Sample Program:
This program creates a file named Testl. Before creating the
file, it checks to see if it already exists. If the file exists, the procedure deletes it. An error trap catches any error that might
occur. To test if the trap works for Error 216, "Pathname not
found", the statement ERROR 216 is inserted as the fourth line.
After testing the ~ trap to make sure it works, delete this line to
use the procedure.
11-45
BASIC09 Reference
PROCEDURE errortest
ODIM PATH,ERRNUM:HYTE; RESPONSE:STRINGL11
OHASE 0
DON ERROR GOTO 10 C* set error trap
pERROR 216 C* simulate error
ODELETE "te5t1"
OGOTO 1 0 0
100ERRNUM-ERR
DI F ERRNUM-216 THEN
OINPUT "File doesn't exi5t...continue?
CY/N)",RESPONSE
OIF RESPONSE-"N" THEN
pEND "Procedure terminated at your request
OENDIF
OENDIF
DON ERROR C* turn off error trap
1 0 00CREATE #PATH , "test 1 ":WRITE
REND
11-46
BASIC09 Commands Reference l 11
EXITIF/THEN/ENDEXIT
Exit from loop if a condition is true
Syntax:
EXITIF condition THEN
statement
ENDEXIT
Function:
Use these statements with loop constructions (particularly LOOP and ENDLOOP) to provide an exit for what is
otherwise an endless loop. EXITIF performs a test of a Boolean expression, such as A<B. The THEN statement precedes
any operation you want to execute if the expression is true.
You must always follow EXITIF with an ENDEXIT.
If the Boolean expression following an EXITIF is false, execution of the program transfers to the statement immediately
following the body of the loop (after the ENDEXIT statement).
Otherwise, BASIC09 executes the statements) between
EXITIF and ENDEXIT, then transfers control to the statement following the body of the loop.
You can also use EXITIF and ENDEXIT with types of loop
constructions other than LOOP/ENDLOOP.
Parameters:
Boolean A
comparison operation that returns either
expression True or False, such as A=B, A<B, or
A=B=C.
statement An operation to be performed if the Boolean
expression tested by EXITIF is True, such as:
PRINT A is less than H.
11-47
BASIC09
Reference
Examples:
LOOP
COUNT=COUNT+1
EXITIF COUNT>100 THEN
DONE = TRUE
ENDEXIT
PRINT COUNT
X = COUNT/2
ENDLOOP
Sample Program:
This procedure simulates a gambling machine by randomly
selecting among several fruit names and displaying them: It
gives you a starting stake of $25 and, depending on the combination of fruit selected, it adds or subtracts from your stake.
If your stake drops to zero, an EXITIF statement ends the procedure and tells you that you're broke.
PROCEDURE onearm
ODIM FRUIT1,FRUIT2,FRUIT3,STAKE:INTEGER; FRUITC8):
STRINGf6J
pSTAKE=25
pPRINT \ PRINT "You have $"; STAKE; " to play
with."
pFOR T=1 TO 8
DREAD FRUITCT)
pNEXT T
pL00P
OFRUIT1=RNDC7)+1 \FRUIT2=RNDC7)+1 \FRUIT3=RNDC7)+1
pPRINT FRUITCFRUIT1); " "; FRUITCFRUIT2); " ";
FRUITCFRUIT3)
pIF FRUITCFRUIT1)=FRUITCFRUIT2) AND FRUITCFRUIT1)=
FRUITCFRUIT3) THEN STAKE=STAKE+10
DELSE
pIF FRUITCFRUIT1)=FRUITCFRUIT2) OR FRUITCFRUIT1)=_
FRUITCFRUIT3) OR
pFRUITCFRUIT2)=FRUITCFRUIT3) THEN
OSTAKE=STAKE+ 1
pELSE
pSTAKE=STAKE-1
REND I F
REND I F
11-4$
BASIC09 Commands Reference l 11
OREM exit play loop i5 Stake i5 le55 than $1.
pEXITIF STAKE<1 THEN
OPRINT
OPRINT "You're Bu5ted...Hetter go home."
DENDEXIT
OPRINT "Your Stake i5 now $"; STAKE; "."
OPRINT
OPRINT
DINPUT "Pre55 ENTER to pull again...",Z$
OENDLOOP
REND
ODATA "ORANGE","APPLE","CHERRY","LEMON","BANANA"
ODATA "PEAR", "PLUM","PEACH"
11-49
BASIC09 Reference
EXP
Return natural exponent
Syntax:
E XP(n um ber)
Function: Returns the natural exponent of
number,
that is, e
(2.71828183) to the power of
number. Number
must be
positive.
This function is the inverse of the
LOG
function. Therefore,
number = EXP(LOG(number)).
Parameters:
number A
positive value.
Examples:
PRINT EXP(2)
Sample Program:
This procedure calculates the exponent of values in the range
0-1.
PROCEDURE exprint
OFOR T=0 TO 1 STEP .03
OPRINT EXP(T),EXP(T+.01),EXP(T+.02)
ONEXT T
REND
11-50