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.
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 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.
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
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.
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.
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.
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:
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.
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.
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.
If you assign the value "ROBERT" to the variable, BASIC09 does not need to terminate the string with 00 because the string is full:
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.
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.
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 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:
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.
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.
A string constant consists of a sequence of characters enclosed in double quotation marks, such as:
To place a string constant in a string type variable, use the equal symbol in this manner:
To include double quotation marks within a string, use two sets of double quotation marks, like this:
A string can contain characters that have ASCII values in the range 0 through 255.
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.
the assign symbol (= ), or by using the assign symbol alone. For instance, both the following command lines are legal:
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.
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:
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.
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:
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.
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:
You can use each of these elements to store a different value, such as:
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.
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.
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.
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:
You can combine all these variables into one complex data type. To do so, dimension the variables within a TYPE command line, like this:
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:
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:
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: