[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP BASIC for OpenVMS
|
Previous | Contents | Index |
The DEF* statement lets you define a single- or multiline function.
The DEF* statement is not recommended for new program development. It is recommended that you use the DEF statement for defining single- and multiline functions. |
- Data-type can be any HP BASIC data type keyword or a data type defined in the RECORD statement. Data type keywords, size, range, and precision are listed in Table 1-2.
- The data type that precedes the def-name specifies the data type of the value returned by the DEF* function.
- Def-name is the name of the DEF* function. The def-name can contain from 1 to 31 characters.
- If the def-name also appears in a DECLARE FUNCTION statement, the following rules apply:
- If the def-name does not appear in a DECLARE FUNCTION statement, but the DEF* statement appears before the first reference to the def-name, the following rules apply:
- The function data type is optional.
- The first character of the def-name must be an alphabetic character (A to Z). The remaining characters can be any combination of letters, digits, dollar signs, underscores, or periods.
- If a function data type is not specified, the last character in the def-name must be a percent sign for an INTEGER function, or a dollar sign for a STRING function.
- If the def-name does not appear in a DECLARE FUNCTION statement, and the DEF* statement appears after the first reference to the def-name, the following rules apply:
- The function data type cannot be present.
- The first two characters of the def-name must be FN. The remaining characters can be any combination of letters, digits, dollar signs, underscores, or periods, with one restriction: the last character must be a percent sign for an INTEGER function, or a dollar sign for a STRING function.
- There must be at least one character between the FN characters and the ending dollar sign or percent character. FN$ and FN% are not valid function names.
- Var specifies optional formal function parameters.
- You can specify the data type of function parameters with a data type keyword. If you do not specify a data type, parameters are of the default type and size. Parameters that follow a data type are of the specified type and size until you specify another data type.
- You can specify up to 8 parameters in a DEF* statement.
- Single-Line DEF*
Exp specifies the operations the function performs.- Multiline DEF*
- Statements specifies the operations the function performs.
- The END DEF or FNEND statement is required to end a multiline DEF*.
- HP BASIC does not allow you to specify any statements that indicate the beginning or end of any SUB, FUNCTION, PICTURE, HANDLER, PROGRAM or DEF in a function definition.
- Exp specifies the function result. Exp must be compatible with the DEF data type.
- When HP BASIC encounters a DEF* statement, control of the program passes to the next executable statement after the DEF*.
- A function defined by the DEF* statement is invoked when you use the function name in an expression.
- You cannot specify how parameters are passed. When you invoke a DEF* function, HP BASIC evaluates parameters from left to right and passes parameters to the function so that they cannot be modified. Numeric parameters are passed by value, and string parameters are passed by descriptor, where the descriptor points to a local copy. A DEF* function can reference variables in the program unit where the function is declared, but it cannot reference variables in other DEF or DEF* functions. A DEF* function can, therefore, modify variables in its program unit, but not variables within another DEF* function.
- The following differences exist between DEF* and DEF statements:
- You can use the GOTO, ON GOTO, GOSUB, and ON GOSUB statements to a branch outside a multiline DEF*, but they are not recommended.
- Although other variables used within the body of a DEF* function are not local to the DEF* function, DEF* formal parameters are. However, if you change the value of formal parameters within a DEF* function and then transfer control out of the DEF* function without executing the END DEF or FNEND statement, variables outside the DEF* that have the same names as DEF* formal parameters are also changed.
- You can pass up to 255 parameters to a DEF function. DEF* functions accept a maximum of 8 parameters.
- A DEF* function value is not initialized when the DEF* function is invoked. Therefore, if a DEF* function is invoked and no new function value is assigned, the DEF* function returns the value of its previous invocation.
- The error handler of the program module that contains the DEF* is the default error handler for a DEF* function. Parameters return to their original values when control passes to the error handler.
- A DEF* is local to the program unit or subprogram that defines it.
- You can declare a DEF* either by defining it, by using the DECLARE FUNCTION statement, or by implicitly declaring it with a reference to the function in an expression.
- If the number of parameters, types of parameters, or type of result declared in the invocation disagree with the number or types of parameters defined in the DEF* statement, HP BASIC signals an error.
- DEF* functions can be recursive.
- DATA statements in a multiline DEF* are not local to the function; they are local to the program module containing the function definition.
- DEF* definitions cannot appear inside a protected region, but they can contain one or more protected regions.
- DEF* functions cannot be invoked within handlers or within DEF functions.
- In DEF* functions that contain handlers, the following rules apply:
- If the function was invoked from a protected region, the EXIT HANDLER statement transfers control to the handler specified for that protected region.
- If the function was not invoked from a protected region, the EXIT HANDLER statement transfers control to the default error handler.
- If a DEF* function is invoked from within a complex expression, the compiler will generate a warning and reorder the expression to evaluate the DEF* function first. This reordering will not effect the outcome of the expression unless the DEF* modifies one of the variables used within the expression.
!Single-Line DEF* DEF* STRING CONCAT(STRING A,B) = A + B DECLARE STRING word1,word2 INPUT "Enter two words";word1,word2 PRINT CONCAT (word1,word2)Output
Enter two words? TO ? DAY TODAY
!multiline DEF* DEF* DOUBLE example(DOUBLE A, B, SINGLE C, D, E) EXIT DEF IF B = 0 example = (A/B) + C - (D*E) END DEF INPUT "Enter 5 numbers";V,W,X,Y,Z PRINT example(V,W,X,Y,Z)Output
Enter 5 numbers? 2,4,6,8,1 -1.5
The DELETE statement removes a record from a relative or indexed file.
Chnl-exp is a numeric expression that specifies a channel number associated with a file. It must be immediately preceded by a number sign (#).
- The DELETE statement removes the current record from a file. Once the record is removed, you cannot access it.
- The file specified by chnl-exp must have been opened with ACCESS MODIFY or WRITE.
- You can delete a record only if the last I/O statement executed on the specified channel was a successful GET or FIND operation.
- The DELETE statement leaves the current record pointer undefined and the next record pointer unchanged.
- HP BASIC signals an error when the I/O channel is illegal or not open, when no current record exists, when access is illegal or illogical, or when the operation is illegal.
DECLARE STRING record_num . . . OPEN "CUS.DAT" FOR INPUT AS #1, RELATIVE FIXED & ACCESS MODIFY, RECORDSIZE 40 . . . INPUT "WHICH RECORD WOULD YOU LIKE TO EXAMINE";record_num GET #1, RECORD record_num DELETE #1 . . .In this example, the file CUS.DAT is opened for input with ACCESS MODIFY. Once you enter the number of the record you want to retrieve and the GET statement executes successfully, the current record number is deleted.
The DET function returns the value of the determinant of the last matrix inverted with the MAT INV function.
None
- When a matrix is inverted with the MAT INV statement, HP BASIC calculates the determinant as a by-product of the inversion process. The DET function retrieves this value.
- If your program does not contain a MAT INV statement, the DET function returns a value of zero.
- The value returned by the DET function is a floating-point value of the default size.
MAT INPUT first_array(3,3) MAT PRINT first_array; PRINT MAT inv_array = INV (first_array) determinant = DET MAT PRINT inv_array; PRINT PRINT determinant PRINT MAT mult_array = first_array * inv_array MAT PRINT mult_array;Output
? 1,0,0,0,1,0,0,0,1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 1 0 0 0 1
The DIF$ function returns a numeric string whose value is the difference between two numeric strings.
Each str-exp can contain up to 60 ASCII digits, an optional decimal point, and an optional leading sign.
- The DIF$ function does not support E-format notation.
- HP BASIC subtracts str-exp2 from str-exp1 and stores the result in str-var.
- The difference between two integers takes the precision of the larger integer.
- The difference between two decimal fractions takes the precision of the more precise fraction, unless trailing zeros generate that precision.
- The difference between two floating-point numbers takes precision as follows:
- The difference of the integer parts takes the precision of the larger part.
- The difference of the decimal fraction part takes the precision of the more precise part.
- HP BASIC truncates leading and trailing zeros.
PRINT DIF$ ("689","-231")Output
920
The DIMENSION statement creates and names a static, dynamic, or virtual array. The array subscripts determine the dimensions and the size of the array. You can specify the data type of the array and associate the array with an I/O channel.
- An array name in a DIM statement cannot also appear in a COMMON, MAP, or DECLARE statement.
- Data-type can be any HP BASIC data type keyword or a data type defined in a RECORD statement. Data type keywords, size, range, and precision are listed in Table 1-2.
- If you do specify a data type and the array name ends in a percent sign (%) or dollar sign ($) suffix character, the variable must be a string or integer data type.
- If you do not specify a data type, the array name determines the type of data the array holds. If the array name ends in a percent sign, the array stores integer data of the default integer size. If the array name ends in a dollar sign, the array stores string data. Otherwise, the array stores data of the default type and size.
- An array can have up to 32 dimensions. Nonvirtual array sizes are limited by the virtual memory limits of your system.
- When you declare a nonvirtual array, HP BASIC allows you to specify both lower and upper bounds. The upper bounds is required; the lower bounds is optional.
- Int-const1 or int-var1 specifies the lower bounds of the array.
- Int-const2 or int-var2 specifies the upper bounds of the array and, when accompanied by int-const1 or int-var1, must be preceded by the keyword TO.
- Int-const1 must be less than or equal to int-const2. Int-var1 must be less than or equal to int-var2.
- If you do not specify int-const1 or int-var1, HP BASIC uses zero as the default lower bounds.
- Array dimensions can have either positive or negative values.
- Nonvirtual, Nonexecutable
- When all the dimension specifications are integer constants, as in DIM A(15,10,20), the DIM statement is nonexecutable and the array size is static. A static array cannot appear in another DIM statement because HP BASIC determines storage requirements at compilation time.
- A nonexecutable DIM statement must lexically precede any reference to the array it dimensions. That is, you must dimension a static array before you can reference array elements.
- Virtual
- The virtual array must be dimensioned and the file must be open before you can reference the array.
- When the data type is STRING, the =int-const clause specifies the length of each array element. The default string length is 16 characters. Virtual string array lengths are rounded to the next higher power of 2. Therefore, specifying an element length of 12 results in an actual length of 16. For example:
DIM #1, STRING vir_array(100) = 12 OPEN "STATS.BAS" FOR OUTPUT as #1, VIRTUAL
Output
%BASIC-W-STRLENINC, virtual array string VIR_ARRAY length increased from 12 to 16- Executable
When any of the dimension specifications are integer variables as in DIM A(10%,20%,Y%), the DIM statement is executable and the array is dynamic. A dynamic array can be redimensioned with a DIM statement any number of times because HP BASIC allocates storage at run time when each DIM statement is executed.
- You can create an array implicitly by referencing an array element without using a DIM statement. This causes HP BASIC to create an array with dimensions of (10), (10,10), (10,10,10), and so on, depending on the number of bounds specifications in the referenced array element. You cannot create virtual or executable arrays implicitly.
- HP BASIC allocates storage for arrays by row, from right to left.
- Nonvirtual, Nonexecutable
- You can declare arrays with the COMMON, MAP, and DECLARE statements. Arrays so declared cannot be redimensioned with the DIM statement. Furthermore, string arrays declared with a COMMON or MAP statement are always fixed-length arrays.
- If you reference an array element declared in an array whose subscripts are smaller than the lower bounds or larger than the upper bounds specified in the DIM statement, HP BASIC signals the error "Subscript out of range" (ERR=55).
- Virtual
- For new development, using virtual arrays is not recommended.
- When the rightmost subscript varies faster than the subscripts to the left, fewer disk accesses are necessary to access array elements in virtual arrays.
- Using the same DIM statement for multiple virtual arrays allocates all arrays in a single disk file. The arrays are stored in the order they were declared.
- Any program or subprogram can access a virtual array by declaring it in a virtual DIMENSION statement. For example:
DIM #1, A(10) DIM #1, B(10)
In this example, array B overlays array A. You must specify the same channel number, data types, and limits in the same order as they occur in the DIM statement that created the virtual array.- HP BASIC stores a string in a virtual array by padding it with trailing nulls to the length of the array element. It removes these nulls when it retrieves the string from the virtual array. Remember that string array element sizes are always rounded to the next power of 2.
- The OPEN statement for a virtual array must include the ORGANIZATION VIRTUAL clause for the channel specified in the DIMENSION statement.
- HP BASIC does not initialize virtual arrays and treats them as statically allocated arrays. You cannot redimension virtual arrays.
- See the HP BASIC for OpenVMS User Manual for more information about virtual arrays.
- Executable
- You create an executable, dynamic array by using integer variables for array bounds, as in DIM A(Y%,X%). This eliminates the need to dimension an array to its largest possible size. Array bounds in an executable DIM statement can be constants or variables, but not expressions. At least one bounds must be a variable.
- You cannot reference an array named in an executable DIM statement until after the DIM statement executes.
- You can redimension a dynamic array to make the bounds of each dimension larger or smaller, but you cannot change the number of dimensions. For example, you cannot redimension a four-dimensional array to be a five-dimensional array.
- The executable DIM statement cannot be used to dimension virtual arrays, arrays received as formal parameters, or arrays declared in COMMON, MAP, or nonexecutable DIM statements.
- An executable DIM statement always reinitializes the array to zero (for numeric arrays) or to the null string if string.
- If you reference an array element declared in an executable DIM statement whose subscripts are not within the bounds specified in the last execution of the DIM, HP BASIC signals the error "Subscript out of range" (ERR=55).
!Nonvirtual, Nonexecutable DIM STRING name_list(20 TO 100), BYTE age(100)
!Virtual DIM #1%, STRING name_list(500), REAL amount(10,10)
!Executable DIM DOUBLE inventory(base,markup) . . . DIM DOUBLE inventory (new_base,new_markup)
The ECHO function causes characters to be echoed at a terminal that is opened on a specified channel.
Chnl-exp must specify a terminal.
- The ECHO function is the complement of the NOECHO function; each function disables the effect of the other.
- The ECHO function has no effect on an unopened channel.
- The ECHO function always returns a value of zero.
DECLARE INTEGER Y, & STRING pass_word Y = NOECHO(0%) SET NO PROMPT INPUT "Enter your password: ";pass_word Y = ECHO(0%) IF pass_word = "Darlene" THEN PRINT CR+LF+"YOU ARE CORRECT !" END IFOutput
Enter your password? YOU ARE CORRECT !
Previous | Next | Contents | Index |