Chapter 6

Data and Variables

Data Types

Data is information on which a computer performs its operations. Data is always numeric but, depending on your computer application, it can represent values, symbols, or alphabetic characters. This means that the same items of physical data can have very different logical meanings, depending on how a program interprets it.


For instance, 65 can represent:



0 The location of a memory address.

The offset of a memory location.

The two character symbols 6 and 5.


Because of the differences in how BASIC09 uses data, the system lets you define five types of data. For instance, there are three ways to represent numbers. Each has its own advantages and disadvantages. The decision to use one way or another depends on the specific program you are developing. The five BASIC09 data types are byte, integer, real, string, and Boolean.


In addition to the preceding data types, there are complex data types you can define. The manual discusses complex data structures at the end of this chapter.


The byte, integer, and real data types represent numbers.

The string data type represents character data (alphabet, punctuation, numeric characters, and other symbols). The default length of strings is 32 characters. Using the DIM statement, you can specify strings of both longer and shorter lengths.


The Boolean data type represents the logical value, TRUE or FALSE.

BASIC09 Reference

You can create arrays (lists) of any of these data types with one, two, or three dimensions. The following table shows the data types and their characteristics:

Memory
Type Allowable Values Requirements
BYTE Whole numbers (0 to 255) One byte
INTEGER whole numbers (-32768 Two bytes
to 32767)
REAL Floating point Five bytes
(± 1*10" ±38)
STRING Letters, digits, One byte per
punctuation character
BOOLEAN True or false One byte

Real numbers appear to be the most versatile. They have the greatest range and are floating point. However, arithmetic operations involving real numbers execute much more slowly than those involving integer or byte values. Real numbers also take up considerably more memory storage space than the other two numeric data types.


Arithmetic involving byte values is not appreciably faster than arithmetic involving integers, but byte data conserves memory.


If you do not specify the type of variable (a symbolic name for a value) in a DIM statement, BASIC09 assumes the variable is real.


The Byte Data Type

Byte variables hold unsigned eight-bit data (integers in the range 0 through 255). Using byte values in computations, BASIC 09 converts the byte values to 16-bit integer values. If you store an integer value that is too large for the byte range, BASIC09 stores only the least-significant eight bits (a value of 255 or less), and does not return an error.


6-2


The Integer Data Type

Integer variables require two bytes (16 bits) of storage. They can fall in the range -32768 to 32767. If a calculation involves both integer values and real values, BASIC09 presents the result of the calculation as a real number.


You can also use hexadecimal values in integer data. To do so, precede the value with the dollar sign ($). For instance, to represent the decimal value 199 as hexadecimal, type $ C 7 . The hexadecimal value range is $0000 through $FFFF.


If you give an integer variable a value that is outside the integer range (greater than 32767 or less than -32768), BASIC09 does not produce an error. Instead it wraps around the value range. For instance, the calculation 32767 + 1 produces a result of -32768.


This means that numeric comparisons made on values in the range 32768 through 65535 deal with negative numbers. You should limit such comparisons to tests for equality or nonequality. Functions such as LAND, LNOT, LOR, and LXOR use integer values but produce results on a non-numeric, bit-by-bit, basis.


Division of an integer by another integer yields an integer. BASIC09 discards any remainder.


The Real Data Type

If you do not assign a data type to a variable, BASIC09 assumes the variable is real. However, programs are easier to understand if you define all variable types.


BASIC09 stores as real values any constants that have decimal points. If a constant does not have a decimal point, BASIC09 stores it as an integer.


BASIC09 requires five consecutive memory bytes to store real numbers. The first byte is the exponent, in binary two's complement. The next four bytes are the binary sign and magnitude of the mantissa. The mantissa is in the first 31 bits; the sign of the mantissa is in the last (least-significant) bit of the last byte. The following illustration shows the memory storage of a real number:



BASIC09 Reference

        Internal Representation of Real Numbers


        exponent mantissa S


    byte: 0 1 2 3 4


The exponent covers the range 2.938735877x10 -3g (2 -128) through 1.701411835x1038 (2127) as powers of 2. Operations that result in values out of the representation range cause an overflow or underflow error. You can handle such errors using the ON ERROR command.


The mantissa covers the range 0.5 through .9999999995 in steps of 2-31. This means that real numbers can represent values .0000000005 apart. BASIC09 rounds operation values that fall between these points to the nearest point.


Because floating point arithmetic is inherently inexact, a sequence of operations can produce a cumulative error. Proper rounding, as implemented in BASIC09, reduces the effect of this problem, but cannot eliminate it. When using real quantities in comparisons, be sure your computations can produce the exact value you desire.


String Variables

A string is a variable-length sequence of ASCII values. The length can vary from 0, a null string, to the capacity of the memory available to BASIC09.


You can define a string variable either explicitly, using the DIM statement, or implicitly by appending the dollar sign ($) to the variable identifier (variable name). For example, title$ implicitly identifies a string variable.


Unless you specify otherwise, BASIC09 assigns a maximum string length of 32 characters. Using the DIM statement, you can specify a maximum length either less than or greater than 32. To conserve memory, use DIM to assign only the maximum length you need for any string variable.


The beginning of a string is always Character 1. The BASE statement, which sets numeric variable base numbers as either 0 or 1, does not affect string variables.


6-4
                      Data and variables / 6


If an operation results in a string too long to fit in the assigned maximum storage space, the system truncates the string on the right. It does not produce an error.


String storage is fixed at the dimensioned length. The sequence of actual string byte values is terminated by the value of zero, or by the maximum length allotted to the string. Any unused storage after the zero byte allows the stored string to expand and contract within its assigned length.


The following example shows the internal storage of a variable dimensioned as 5 t r i n 9 161 and assigned the value "SAM". Note that Byte 3 contains the string terminator 00. The string does not use bytes following 00.


          S A M 00


    byte: 1 2 3 4 5 6


If you assign the value "ROBERT" to the variable, BASIC09 does not need to terminate the string with 00 because the string is full:


,

          R O B E R T


    byte: 1 2 3 4 5 6


The way BASIC09 handles string storage is important when you write programs. If you do not specify a length for strings you define, the system uses the default length 32. As you can see, this wastes computer memory if you store strings of only four or five characters.


The Boolean Type

A Boolean operation always returns either the character string "TRUE" or "FALSE". You cannot use the Boolean data type for numeric computation only for comparison logic.


Do not confuse the Boolean operations AND, OR, XOR, and NOT (which operate on the Boolean values TRUE and FALSE) with the logical functions LAND, LOR, LXOR, and LNOT (which use integer values to produce numeric results on a bit-by-bit basis). An attempt to store a non-Boolean value in a Boolean variable, causes an error.


6-5
BASIC09 Reference

Automatic Type Conversion

When an operation mixes numeric data types (byte, integer, or real values), BASIC09 automatically and temporarily converts the values to the type necessary to retain accuracy. This conversion lets you use numeric quantities of mixed types in most calculations.


The system returns a type-mismatch error when an expression includes types that cannot legally mix. These errors are reported by the second compiler pass, which occurs automatically when you exit the edit mode.


Because type conversion takes additional execution time, you can speed calculations by using values of a single type.


Constants

Constants are values in a program that do not change. They can use any of the five data types. The following are examples of constants in a procedure:


    HOMES="Fort Worth"

    VALUES="$25,000

    VALUE=25

    PAYMENT=99.99

    ANSWER="TRUE"

    MEMORY=$OCFF

    PRINT "The End"


Numeric constants are either integers or real numbers. If a numeric constant includes a decimal point or uses the "E format" exponential form, it causes BASIC09 to store the number in the real format, even if it could store the number in integer or byte format.


You can use this feature to force a real format. For instance, to make the number 12 a real number, type it as 12.0. You might want to force real values in this way when all other values in an expression are real so that BASIC09 does not have to do a timeconsuming type conversion at run time.


6-6
Data and variables / 6

BASIC09 also stores as real numbers any numbers that do not have decimal points but that are too large to store as integers. Here are some examples of legal real constants:

1.0

-999.000099 100000000

1.95E + 12

9.8433218 -.01

5644.34532

-99999.9E-33

BASIC09 treats numbers that do not have a decimal point and are in the range -32768 through + 32767 as integers. You must always precede hexadecimal numbers with a dollar sign.


Following are examples of legal integer constants:

    12 -3000 55

    $20 $FF $09

    0 -12 -32768


String Constants

A string constant consists of a sequence of characters enclosed in double quotation marks, such as:


"The End"

To place a string constant in a string type variable, use the equal symbol in this manner:


TITLES _ "Masters Of Magic"

To include double quotation marks within a string, use two sets of double quotation marks, like this:


"An ""older man"" i5 wiser."

A string can contain characters that have ASCII values in the range 0 through 255.


Variables

In BASIC09, a variable is local to the procedure in which it is defined. A variable defined in one procedure has no meaning in another procedure unless you use the RUN and PARAM statements to pass the variable when you call the other procedure.


The local nature of variables lets you use the same variable name in more than one procedure and, unless you specify otherwise, have the variables operate independently of each other.


6-7
BASIC09 Reference

You can assign variables using either the LET statement with

the assign symbol (= ), or by using the assign symbol alone. For instance, both the following command lines are legal:


    LET PAYMENT=44.50 PAYMENT=44.50


When you call a procedure, BASIC09 allocates storage for the procedure's variables. It is not possible to force a variable to occupy an absolute address in memory. When you exit a procedure, the system returns the storage allotted for variables, and you lose the stored values.


If you write a procedure to call itself (a recursive procedure), the call creates separate storage space for variables.


Note: Unlike other BASICS, BASIC09 does not automatically initialize variables by setting them to zero. When you execute a procedure, all variables, arrays, and structures have random values. Your procedure must initialize the variables you specify to the values you require.

Passing Variables

When one procedure passes variable values to another procedure, BASIC09 refers to the passed variables as parameters. You can pass variables either by reference or by value.


BASIC09 does not protect variables passed by reference. Therefore, the called procedure can change the values and return the new values. BASIC09 does protect variables passed by value, so, the called program cannot change them.


To pass a parameter by reference, enclose the name of the variable in parentheses as part of the RUN statement in this manner:


    RUN RANDOM (1 0) passes the value 10 to a procedure

    called Random _


The system evaluates the storage address of each passed variable, and sends the variable to the called procedure. The called procedure associates the storage addresses with the names in its local PARAM statement. It then uses the storage area as though it had created it locally. This means it can change the value of the parameter before returning it to the calling procedure.


6-8
Data and variables / 6

To pass parameters by value, write the value to be passed as an expression. BASIC09 evaluates the expression at the time of the call. To use a variable in an expression without changing its value, use null constants, such as 0 for a number or I'll for a string, in this manner:


    RUN ADDCOLUMNCx+O) passes the value of x by

    value

    RUN TRANSLATEtw$+"") passes the contents of w$ by

    value


To pass parameters by value, BASIC09 creates a temporary variable. It places the result of the expression in the temporary variable and sends the address to the called procedure. This means that the value given to the called procedure is a copy of the result of the expression, and the called procedure cannot change the original value.


The results of expressions containing numeric constants are either integer or real values; there are no byte constants. To send byte-type variables to a procedure, pass the values by reference. Therefore, if a RUN statement evaluates an integer as a parameter and sends it to a byte-type variable, the byte variable uses only the high-order byte of the two-byte integer.


Arrays

An array is a group of related data values stored consecutively in memory. The system knows the entire group by a variable name. Each data value is an element. You use a subscript to refer to any element of the array. For example, an array named Graf might contain five elements referred to as:


    GRAFM GRAFC2) GRAFC3) GRAFM GRAFC5)


You can use each of these elements to store a different value, such as:


    GRAFC1) = 25


    GRAFM = 47


    GRAFC3) = 39


    GRAFM = 18


    GRAFCS) = 50


6-9
BASIC09 Reference

Note: Normally, array elements start with 1 in BASIC09. However, you can use the BASE command to cause array elements to begin at 0.

The previous example illustrates a single-dimensioned array. The elements are arranged in one row and only one subscript is used for each element.

The following procedure lets you type values for a GRAF array, and displays the results in a simple graph.

PROCEDURE GRAF
ODIM GRAFCS):REAL
pSHELL "DISPLAY 0C"
OFOR T=1 TO 5
OPRINT "Value for Item *"; T; "O";
pINPUT GRAFCt)
ONEXT T
OPRINT
OPRINT
OPRINT "This 15 how your graph Stacks up..."
OPRINT
OFOR T=1 TO 5
OPRINT "Item *"; T; "O";
OFOR U=1 TO GRAFCT)
OPRINT CHR$C79);
ONEXT U
OPRINT
ONEXT T
OPRINT
REND

This program uses a single dimension array in effect, a list.

You can also create arrays with more than one dimension
more than one element for each row. You might use a two-dimen
sioned array in a program to store names and addresses. Instead
of creating separate arrays for the name, address, and zip code,
you could set up one array with two dimensions. -

6-10
Data and variables / 6

The following program, used to enter the names of a company's employees, shows how this might be done. See the second line for the DIM syntax. When you run the procedure, it asks you for a name, address, and zip code for each of 10 employees. After you type the information for all the entries, the procedure displays the information on the screen.


PROCEDURE Names
ODIM NAMEC10,3):STRING
OSHELL "DISPLAY 0C"
OHASE 0
OFOR T=0 TO 9
OPRINT "Type Employee Name No."; T; ": ";
OINPUT NAMECT,O)
OPRINT "Type Employee Addre55 No."; T; ": ";
DI NPUT NAMECT,1 )
OPRINT "Type Employee Zip Code No."; T; ": ";
DINPUT NAMECT,2)
ONEXT T
OSHELL "DISPLAY 0C"
OPRINT "And the names are..."
OPRINT
OFOR T=0 TO 9
OPRINT NAMECT,O); "O"; NAMECT,1 ); "O"; NAMECT,2)
ONEXT T
FIEND

The DIM statement reserves space in memory for a string array named Name, with two dimensions. As you enter data, the Name field is stored in Name C 0 , 0 ), Name C 1 , 0 ), Name C 2 , 0 ), and so on. The Address field is stored in N a m e c 0 , 1 ) , N a m e c 1 , 1 ) , N a m e c 2 ,1 ) , and so on. The Zip field is stored in N a m e c 0 , 2) , N a m e c 1 , 2) , N a m e c 2 , 2) , and so on. This continues until you fill the grid, 10 entries with three items each.


You can also create arrays with three dimensions. The following program adds one more dimension that keeps track of each employee's company. It dimensions Names as N a m e $ C 2 ,1 0 ; 3) . The first dimension contains either 0 or 1 to indicate to which company the employee belongs.


6-11
BASIC09 Reference

PROCEDURE name52 ODIM NAME$C2,10,3):STRING OSHELL "DISPLAY 0C" OHASE 0 OFOR X=0 TO 1 OPRINT pPRINT OFOR T=0 TO 9 OPRINT OIF X=0 THEN OPRINT "Type a Wiggleworth Company employee name..." DELSE OPRINT "Type a Putforth Company employee name..." OENDIF OPRINT "Type Name No."; T; ": "; OINPUT NAME$CX,T,O) OPRINT "Type Addre55 No."; T; ": "; DINPUT NAME$CX,T,1 ) OPRINT "Type Zip Code No."; T; ": "; DINPUT NAME$CX,T,2) ONEXT T ONEXT X OSHELL "DISPLAY 0C" OPRINT "The Wiggleworth employees are..." OPRINT OX=0 OFOR T=0 TO 9 OPRINT NAME$ CX,T,0); "O"; NAME $CX,T,1 ); "O"; NAME$CX,T,2) ONEXT T OPRINT OPRINT "The Putforth Company employees are..." OPR I NT OX=1 OFOR T=0 TO 9
OPRINT NAME$CX,T,O); "O"; NAME$CX,T,1 ); "O"; -
NAME$CX,T,2)
ONEXT T
REND

s-12
Data and variables / 6

The easiest way to understand three dimensional arrays is to consider the first dimension as a page. In other words, if the first dimension in the string is 0, the employee is on the Wiggleworth Company's page. If the first dimension in the string is 1, the employee is on the Putforth Company's page.


Complex Data Types

In addition to the five standard data types, you can create your own data types. Using the TYPE command, you can define a new data type as a vector (a single-dimensioned array) of any previously defined type.


For example, in the previous program, the Name variable can contain only one type of data, the string type. However, using the TYPE command you can create a variable that accepts several data types.


Suppose you create an employee list procedure that uses the following variables, of the following size and types:


Name Length Contents Type

Name 25 employee name string
Street 20 street address string
city 10 city of address string
Zip address zip code integer
Sex false = male, true = female Boolean
Age employee age byte

You can combine all these variables into one complex data type. To do so, dimension the variables within a TYPE command line, like this:


TYPE EMPLOYEE=NAME:STRING[25J; STREET:STRINGL20J; CITY:STRINGL10J; ZIP:REAL; SEX :BOOLEAN; AGE:HYTE

This creates a new BASIC09 type, called Employee. Employee requires its variables to have six fields of the name, size, and type shown in the previous chart.


Once you create the new data type, you can define variables to use it. For instance, the following program line defines Worker as type employee, with 10 elements in the array:


DIM WORKERC10):EMPLOYEE

                                6-13

BASIC09 Reference

To put the employee data type to work, collect your data with INPUT commands. Then, store the data into the new Worker array. The following program demonstrates how you might do this:

PROCEDURE worker
OREM Dimension variables for input
pDIM NM:STRINGI251
ODIM ST:STRINGI20J
OD I M CTY : STR I NG L 1 0 J
pDIM ZP:REAL
ODIM SX:BOOLEAN
ODIM AG:BYTE
OREM Create new type and array using new

type
OTYPE EMPLOYEE=NAME:STRINGL25J; STREET:STRINGL20J;
CITY:STRINGL10 l; ZIP:REAL; SEX:BOOLEAN; AGE:BYTE
ODIM WORKERC10):EMPLOYEE
OREM
OFOR T=1 TO 1 0
DI NPUT "Name :O" , NM
DINPUT "Street :O",ST
DINPUT "City:O",CTY
DINPUT "Zip:O",ZP
DI NPUT "Sex :O" , S X
DI NPUT "Age :O" , AG
OREM Store input in the Worker array using
field names
OWORKERCT).NAME=NM
OWORKERCT).STREET=ST
OWORKERCT).CITY=CTY
OWORKERCT).ZIP=ZP
OWORKERCT).SEX=SX
OWORKERCT).AGE-AG
OPRINT
OPRINT
OPRINT
ONEXT T
OSHELL "DISPLAY COC)"
OPRINT "The names in your filer. now are..."
OPRINT
OFOR T=1 TO 1 0
OPRINT WORKERCT).NAME
OPRINT WORKERCT).STREET
OPRINT WORKERCT).CITY

6-14
Data and variables / 6

OPRINT WORKER(T).ZIP
OIF WORKER(T).SEX=TRUE
OTHEN PRINT "Female"
OELSE
OPRINT "Male"
REND I F
OPRINT WORKER(T).AGE
OPRINT
OPR I NT
OPRINT
ONEXT T

Note that the Sex field is defined as Boolean. This means that you can respond only in two ways, TRUE or FALSE. The method of input requires only one byte of storage. To use this data you need to handle it so TRUE and FALSE indicate female and male.


Complex data types can contain more than one field. Each field can be of any data type. You reference the fields of a complex data type by typing the variable name, its array index, a period (.), and the field name. The following lines, from the Worker procedure, show how BASIC09 stores the data from the input lines into the Worker variable:


WORKER(T).NAME=NM
WORKER(T).STREET=ST
WORKER(T).CITY=CTY
WORKER(T).ZIP=ZP
WORKER(T).SEX=SX
WORKER(T).AGE=AG

These lines store the values in the variables NM, ST, CTY, ZP, SX, and AG into the NAME, STREET, CITY, ZIP, SEX, and AGE fields of the Worker variable. This operation is within a FOR/NEXT loop that uses T as a counter. In the procedure, T can refer to a value in the range 1 to 10.


The procedure uses the same type of operation to extract the data from the complex data type variable:


6-15
BASIC09 Reference

PRINT WORKERCT).NAME
PRINT WORKERCT).STREET
PRINT WORKERCT).CITY
PRINT WORKERCT).ZIP
IF WORKERCT).SEX=TRUE THEN PRINT "Female"
ELSE PRINT "Male"
ENDIF
PRINT WORKERCT)AGE
Using the same methods, you can create complex data types that
combine other complex data types and standard data types.
The elements of a complex structure can be copied to another
similar structure. Using a single assignment operator, you can
write an entire structure to, or read an entire structure from,
mass storage as a single entity. Fbr example:
PUT #2, WORKERCT)
Because the system defines the elements of complex-type storage
during compilation, it need not do so during runtime. This
means that BASIC09 can reference complex structure faster than
it can reference arrays. t

6-16