Previous | Contents | Index |
You can implicitly assign a data type to program values by adding a suffix to the variable name or constant value. If you do not specify any suffix, the variable or constant is assigned the current default data type. The following rules apply for implicit data typing:
With implicit data typing, the range and precision for program values are determined by the following corresponding default data sizes or subtypes:
If you do not specify a value for the default data type, REAL will be assigned.
The BASIC qualifiers for the SET and COMPILE commands are
described in Chapter 2. The qualifiers for the DCL command BASIC
are listed in the Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual.
1.3.2 Explicit Data Typing
Explicit data typing means that you use a declarative statement to specify the data type, range, and precision of your program variables and named constants.
In the following example, the first DECLARE statement associates the string constant value 03060 and the STRING data type with a constant named zip_code. The second DECLARE statement associates the STRING data type with emp_name, the DOUBLE data type with with_tax, and the SINGLE data type with int_rate. No constant values are associated with identifiers in the second DECLARE statement because they are variable names.
DECLARE STRING CONSTANT zip_code = "03060" DECLARE STRING emp_name, DOUBLE with_tax, SINGLE int_rate |
With explicit data typing, each program variable within a program can have a different data type. You can explicitly assign data types to variables, constants, arrays, parameters, and functions; therefore, integer data does not have to take the compilation default types. Explicit data typing gives you more control over your program.
Using the REAL and INTEGER keywords to explicitly type program values
allows you to write programs that are more flexible, because these data
type keywords specify that floating-point and integer data take the
current defaults for REAL and INTEGER. The data type INTEGER, for
example, specifies only that the constant or variable is an integer.
The actual subtype (BYTE, WORD, LONG, or QUAD) depends on the default
set with the COMPILE or SET command, with the DCL command BASIC, or
with the OPTION statement.
1.3.3 QUAD and IEEE Floating-Point Data Types
Alpha BASIC adds support for four data types as of Version 1.4. To use these data types, you must have OpenVMS Alpha Version 7.1 or higher. The data types are QUAD, for 64-bit integers, and three IEEE floating-point types: SFLOAT, TFLOAT, and XFLOAT, which correspond to the S_floating, T_floating, and X_floating formats, respectively. VAX BASIC does not support these four data types.
The three formats S_floating, T_floating, and X_floating are for finite values with normal rounding and standard exception handling only. All the floating point formats previously available in Alpha BASIC are still available. QUAD and the IEEE data types are available wherever the other Alpha BASIC formats are available, as detailed in the following sections.
The QUAD keyword is added to the allowed values of the /INTEGER_SIZE qualifier, and the SFLOAT, TFLOAT, and XFLOAT keywords are added to the allowed values of the /REAL_SIZE qualifier.
Statements, Expressions, Functions, and Operators
QUAD, SFLOAT, TFLOAT, and XFLOAT can be used in the following statements wherever a data type is supplied:
Statement | Elements to Which Data Type Is Applied |
---|---|
COMMON | Variables and FILL elements |
DECLARE | Variables, CONSTANTs, and FUNCTION parameters and value |
DEF, DEF* | Parameters and value |
DIMENSION | Variables |
EXTERNAL | Variables, CONSTANTs, and SUB/FUNCTION parameters and value |
FUNCTION | Parameters and value |
MAP | Variables and FILL elements |
MAP DYNAMIC | Variables |
MOVE | FILL elements |
OPTION | Integer and real clauses |
RECORD/GROUP | Record components |
REMAP | FILL elements |
SUB | Parameters |
Expressions with values of these data types can be used in the following statements wherever numeric values are accepted:
CAUSE ERROR, DATA, DET, END, EXIT, FIELD, FIND, FNEND, FNEXIT, FOR, FUNCTIONEND, FUNCTIONEXIT, GET, IF, INPUT, LET, MAT +, MAT -, MAT *, MAT CON, MAT IDN, MAT INPUT, MAT INV, MAT LINPUT, MAT NUL$, MAT PRINT, MAT READ, MAT TRN, MAT ZER, NEXT, ON GOSUB, ON GOTO, OPEN, PRINT, PRINT USING, PUT, READ, RESET, RESTORE, SELECT, SLEEP, UNLESS, UNTIL, UPDATE, WAIT, WHILE
The channel number expression for the following I/O statements and functions is extended to include these data types:
BUFSIZ, CCPOS, CLOSE, DELETE, ECHO, FIELD, FIND, FREE, FSP$, GET, GETRFA, INKEY$, INPUT, INPUT LINE, LINPUT, MAGTAPE, MAR, MARGIN, MAT INPUT, MAT LINPUT, MAT PRINT, NOECHO, NOMARGIN, OPEN, PRINT, PRINT USING, PUT, RCTRLO, RESET, RESTORE, RMSSTATUS, SCRATCH, UNLOCK, UPDATE
In Alpha BASIC, the function INTEGER, besides accepting either a numeric string or any numeric data type expression for the first argument, includes QUAD in the possible data types for the second argument. The function REAL has SFLOAT, TFLOAT, and XFLOAT added to possible data types for its second argument.
All the built-in functions that accept and/or return numerical values allow QUAD and the IEEE data types as appropriate. These include the standard mathematical functions:
ABS, ABS%, ATN, COS, EXP, LOG, LOG10, MAG, MAX, MIN, MOD, SGN, SIN, SQR, TAN
They also include the following miscellaneous functions:
ASCII, CCPOS, CHR$, COMP%, CTRLC, CVT$$ (EDIT$), DATE$, DATE4$, DECIMAL, ECHO, ERT$, FIX, FORMAT$, INKEY$, INSTR, INT, INTEGER, LBOUND, LEFT$, MAGTAPE, MARGIN, MID$, NOECHO, NUM, NUM2, NUM$, NUM1$, PLACE$, POS, PROD$, QUO$, RAD$, RCTRLC, RCTRLO, REAL, RIGHT$, SEG$, SPACE$, STR$, STRING$, SWAP%, TAB, TIME, TIME$, UBOUND, VAL, VAL%
All operators that accept numeric arguments allow the new data types. These include:
unary: +, -
binary: +, -, *, /, ^, <, =, >, =<, =>, <>, == (fuzzy equals)
The explicit literal notation is extended to allow representation of constants of the new data types. See Section 1.5.4.
Data Type Results in Expressions with Operands of Different Types
See Section 1.6.1.1 and Section 1.6.1.2 for the rules determining the data types of results in expressions with operands of different data types, including the new Alpha BASIC data types.
Array subscripts may be of any numeric data type, but must evaluate to
an integer value at run time.
1.4 Variables
A variable is a named quantity whose value can change during program execution. Each variable name refers to a location in the program's storage area. Each location can hold only one value at a time. Variables of all data types can have subscripts that indicate their position in an array. You can declare variables implicitly or explicitly.
Depending on the program operations specified, the value of a variable can change from statement to statement. BASIC uses the most recently assigned value when performing calculations. This value remains in effect until a new value is assigned to the variable.
BASIC accepts the following general types of variables:
The name given to a variable depends on whether the variable is internal or external to the program and whether the variable is implicitly or explicitly declared.
All variable names must conform to the following rules:
BASIC accepts the following implicitly declared variables:
The name of an implicitly declared variable defines its data type. Integer variables end with a percent sign (%), string variables end with a dollar sign ($), and variables of the default type (usually floating-point) end with any allowable character except a percent sign or dollar sign. All three types of variables must conform to the rules listed in Section 1.4.1 for naming variables. The current data type default (INTEGER, REAL, or DECIMAL) determines the data type of implicitly declared variables that do not end in a percent sign or dollar sign.
A floating-point variable is a named location that stores a floating-point value. The current default size for floating-point numbers (SINGLE, DOUBLE, GFLOAT, HFLOAT, SFLOAT, TFLOAT, or XFLOAT) determines the data type of the floating-point variable. Following are some examples of valid floating_point variable names:
C
M1
F67T_J
L...5
BIG47
Z2.
ID_NUMBER
STORAGE_LOCATION_FOR_XX
STRESS_VALUE
If a numeric value of a different data type is assigned to a floating-point variable, BASIC converts the value to a floating-point number.
An integer variable is a named location that stores an integer value. The current default size for integers (BYTE, WORD, LONG, or QUAD) determines the data type of an integer variable. Following are some examples of valid integer variable names:
ABCDEFG%
B%
C_8%
D6E7%
RECORD_NUMBER%
THE_VALUE_I_WANT%
If the default or explicitly declared data type is INTEGER, the percent suffix (%) is not necessary.
If you assign a floating-point or decimal value to an integer variable, BASIC truncates the fractional portion of the value. It does not round to the nearest integer. For example:
B% = -5.7 |
BASIC assigns the value -5 to the integer variable, not -6.
A string variable is a named location that stores strings. Following are some examples of valid string variable names:
C1$
L_6$
ABC1$
M$
F34G$
T..$
EMPLOYEE_NAME$
TARGET_RECORD$
STORAGE_SHELF_IDENTIFIER$
If the default or explicitly declared data type is STRING, the dollar suffix ($) is not necessary.
Strings have both value and length. BASIC sets all string variables to a default length of zero before program execution begins, with the exception of those variables in a COMMON, MAP, virtual array, or record definition. See the COMMON statement and the MAP statement in Chapter 4 for information about string length in COMMON and MAP areas. See the Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual for information about default string length in virtual arrays.
During execution, the length of a character string associated with a
string variable can vary from zero (signifying a null or empty string)
to 65,535 characters.
1.4.3 Explicitly Declared Variables
BASIC lets you explicitly assign a data type to a variable or an array. For example:
DECLARE DOUBLE Interest_rate |
Data type keywords are described in Section 1.3. For more information
about explicit declaration of variables, see the COMMON, DECLARE,
DIMENSION, DEF, FUNCTION, EXTERNAL, MAP, and SUB statements in
Chapter 4.
1.4.4 Subscripted Variables and Arrays
A subscripted variable references part of an array. Arrays can be of any valid data type. Subscripted variables and arrays follow the same naming conventions as unsubscripted variables. Subscripts follow the variable name in parentheses and define the variable's position in the array. When you create an array, you specify the maximum size of the array (the bounds) in parentheses following the array name.
In Example 1-2, the DECLARE statement sets the bounds of the arrayemp_name to 1000. Therefore, the maximum value for an emp_name subscript is 1000. The bounds of the array define the maximum value for a subscript of that array.
Example 1-2 Using the DECLARE Statement to Set Array Boundaries |
---|
DECLARE STRING emp_name(1000) FOR I% = 0% TO 1000% INPUT "Employee name";emp_name(I%) NEXT I% |
Subscripts can be any positive LONG integer value between 0 and 2147483647.
An array is a set of data ordered in one or more dimensions. A one-dimensional array, like emp_name(1000), is called a list or vector. A two-dimensional array, like payroll_data(5,5), is called a matrix. An array of more than two dimensions, like big_array(15,9,2), is called a tensor.
As a default, BASIC arrays are always zero-based. The number of elements in any dimension includes element number zero. For example, the array emp_name contains 1001 elements because BASIC allocates element zero. Payroll_data(5,5) contains 36 elements because BASIC allocates row and column zero.
Often, however, applications call for arrays that are not zero-based. In BASIC, you can define arrays that are not zero-based by specifying a lower bound, as well as an upper bound, for the subscripts. In this way, you can create an array with arbitrary starting and ending points. For example, you might want to create array birth_rate that holds the annual birth rate statistics for the years 1950 to 1985:
DECLARE birth_rate(1950 TO 1985) |
Lower bounds are not allowed with virtual arrays or arrays used in MAT statements. If a multidimensional array is declared with lower bounds specified for some dimensions and not others, zero will be used for those dimensions without lower bounds.
You can use the UBOUND and LBOUND functions to determine the upper and lower bounds of an array. For a description of these functions, see Chapter 4.
For all arrays except virtual arrays, the total number of array elements cannot exceed 2147483647. Note, however, that this is a theoretical value; the actual maximum size of an array that you can declare depends on the configuration of your system.
BASIC arrays can have up to 32 dimensions. You can specify the type of data the array contains with data type keywords. See Table 1-2 for a list of BASIC data types.
An element in a one-dimensional array has a variable name followed by one subscript in parentheses. You may optionally use a space between the array name and the subscript. For example:
A(6%) B (6%) C$ (6%) |
A(6%) refers to the seventh item in this list:
A(0%) A(1%) A(2%) A(3%) A(4%) A(5%) A(6%) |
An element in a two-dimensional array has two subscripts, in parentheses, following the variable name. The first subscript specifies the row number and the second subscript specifies the column number. Use a comma to separate the subscripts. You may optionally put a space between the array name and the subscripts. For example:
A (7%,2%) A%(4%,6%) A$ (10%,10%) |
In Figure 1-1, the arrow points to the element specified by the subscripted variable A%(4%,6%).
Figure 1-1 Representation of the Subscript Variable A%(4%,6%)
Although a program can contain a variable and an array with the same name, this is poor programming practice. Variable A and the array A(3%,3%) are separate entities and are stored in completely separate locations, so it is a good idea to give them different names.
Note that a program cannot contain two arrays with the same name but a different number of subscripts. For example, the arrays A(3%) and A(3%,3%) are invalid in the same program.
BASIC arrays can be redimensioned at run time. See the
Compaq BASIC for OpenVMS Alpha and VAX Systems User Manual for more information about arrays.
1.4.5 Initialization of Variables
BASIC generally sets variables to zero or null values at the start of program execution. Variables initialized by BASIC include:
BASIC does not initialize the following:
A constant is a numeric or character literal that does not change during program execution. A constant may optionally be named and associated with a data type. BASIC allows the following types of constants:
A constant of any of the above data types can be named with the DECLARE CONSTANT statement. You can then refer to the constant by name in your program. See Section 1.5.3 for information about naming constants.
You can use the OPTION statement to declare a default data type for all constants in your program. This statement allows you to specify a data type for only the constants in your program; you can specify a different data type for variables. You can also use a special numeric literal notation to specify the value and data type of a numeric literal. Numeric literal notation is discussed in Section 1.5.4.
If you do not specify a data type for a numeric constant with the DECLARE CONSTANT statement or with numeric literal notation, the type and size of the constant is determined by the default REAL, INTEGER, or DECIMAL type set with the DCL command BASIC, the BASIC SET or COMPILE command, or the OPTION statement.
To simplify the representation of certain ASCII characters and mathematical values, BASIC also supplies some predefined constants.
The following sections discuss numeric and string constants, named
constants, numeric literal notation, and predefined constants.
1.5.1 Numeric Constants
A numeric constant is a literal or named constant whose value never changes. In BASIC, a numeric constant can be a floating-point number, an integer, or a packed decimal number. The type and size of a numeric constant is determined by the following:
If you use a declarative statement to name and declare the data type of a numeric constant, the constant is of the type and size specified in the statement. For example:
DECLARE BYTE CONSTANT age = 12 |
This example associates the numeric literal 12 and the BYTE data type with the identifier age. To specify a data type for an unnamed numeric constant, you must use the numeric literal notation format described in Section 1.5.4.
Previous | Next | Contents | Index |