Compaq BASIC for OpenVMS
Alpha and VAX Systems
Reference Manual


Previous Contents Index


-1            0            123            123 

In this example, BASIC truncates the LONG value assigned to YYY to a BYTE value.

Note

Data types specified in a DECLARE statement override any defaults specified in COMPILE command qualifiers or OPTION statements.


Examples

Example 1


!DEF Functions 
DECLARE INTEGER FUNCTION amount(,,DOUBLE,BYTE,,) 

Example 2


!Named Constants 
DECLARE DOUBLE CONSTANT interest_rate = 15.22 


DEF

The DEF statement lets you define a single-line or multiline function.

Format



Syntax Rules

  1. Data-type can be any 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.
  2. The data type that precedes the def-name specifies the data type of the value returned by the DEF function.
  3. Def-name is the name of the DEF function. The def-name can contain from 1 to 31 characters.
  4. If the def-name also appears in a DECLARE FUNCTION statement, the following rules apply:
  5. 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:
  6. 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:
  7. Var specifies optional formal DEF parameters. Because the parameters are local to the DEF function, any reference to these variables outside the DEF body creates a different variable.
  8. You can specify the data type of DEF parameters with a data type keyword or with a data type defined in a RECORD statement. If you do not include a data type, the parameters are of the default type and size. Parameters that follow a data type keyword are of the specified type and size until you specify another data type.
  9. You can specify up to 255 parameters in a DEF statement.
  10. Single-Line DEF
    Exp specifies the operations the function performs.
  11. Multiline DEF

Remarks

  1. When BASIC encounters a DEF statement, control of the program passes to the next executable statement after the DEF.
  2. The function is invoked when you use the function name in an expression.
  3. You cannot specify how parameters are passed. When you invoke a function, 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 that are declared within the compilation unit in which the function resides, but it cannot reference variables in other DEF or DEF* functions. A DEF function can, therefore, modify other variables in the program, but not variables within another DEF function.
  4. A DEF function is local to the program, subprogram, function, or picture that defines it.
  5. 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.
  6. If your program invokes a function with a name that does not start with FN before the DEF statement defines the function, BASIC signals an error.
  7. 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, BASIC signals an error.
  8. DATA statements in a multiline DEF are not local to the function; they are local to the program module containing the function definition.
  9. The function value is initialized to zero or the null string each time you invoke the function.
  10. DEF definitions cannot appear inside a protected region. However, DEF can contain one or more protected regions.
  11. In DEF definitions that contain handlers, the following rules apply:
  12. If an exception is not handled within a DEF function, control is transferred to the module that invoked the DEF function.
  13. ON ERROR statements within a DEF function are local to the function.
  14. A CONTINUE, GOTO, GOSUB, ON ERROR GOTO, or RESUME statement in a multiline function definition must refer to a line number or label in the same function definition.
  15. You cannot transfer control into a multiline DEF except by invoking the function.
  16. DEF functions can be recursive. However, BASIC does not detect infinitely recursive DEF functions during compilation.

Examples

Example 1


!Single-Line DEF 
DEF DOUBLE add (DOUBLE A, B, SINGLE C, D, E) = A + B + C + D + E 
INPUT 'Enter five numbers to be added';V,W,X,Y,Z 
PRINT 'The sum is';ADD(V,W,X,Y,Z) 

Output


Enter five numbers to be added? 1,2,3,4,5 
The sum is 15 

Example 2


PROGRAM I_want_a_raise 
 
        OPTION TYPE = EXPLICIT,                                    & 
               CONSTANT TYPE = DECIMAL,                            & 
               SIZE = DECIMAL (6,2) 
 
        DECLARE DECIMAL CONSTANT Overtime_factor = 0.50 
        DECLARE DECIMAL My_hours, My_rate, Overtime 
        DECLARE DECIMAL FUNCTION Calculate_pay (DECIMAL,DECIMAL) 
 
        INPUT "Your hours this week";My_hours 
        INPUT "Your hourly rate";My_rate 
 
        PRINT "My pay this week is"; Calculate_pay ( My_hours, My_rate ) 
 
        DEF DECIMAL Calculate_pay (DECIMAL Hours, Rate) 
 
           IF Hours = 0.0 
           THEN 
                EXIT DEF 0.0 
           END IF 
 
           Overtime = Hours - 40.0 
 
           IF Overtime < 0.0 
           THEN 
                Overtime = 0.0 
           END IF 
 
       END DEF  (Hours * Rate) + (Overtime * (Overtime_factor * Rate) ) 
 
END PROGRAM 

Output


Your hours this week? 45.7 
Your pay rate? 20.35 
Your pay for the week is 987.95 


DEF*

The DEF* statement lets you define a single- or multiline function.

Note

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.

Format



Syntax Rules

  1. Data-type can be any 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.
  2. The data type that precedes the def-name specifies the data type of the value returned by the DEF* function.
  3. Def-name is the name of the DEF* function. The def-name can contain from 1 to 31 characters.
  4. If the def-name also appears in a DECLARE FUNCTION statement, the following rules apply:
  5. 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:
  6. 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:
  7. Var specifies optional formal function parameters.
  8. 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.
  9. You can specify up to 8 parameters in a DEF* statement.
  10. Single-Line DEF*
    Exp specifies the operations the function performs.
  11. Multiline DEF*

Remarks

  1. When BASIC encounters a DEF* statement, control of the program passes to the next executable statement after the DEF*.
  2. A function defined by the DEF* statement is invoked when you use the function name in an expression.
  3. You cannot specify how parameters are passed. When you invoke a DEF* function, 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.
  4. The following differences exist between DEF* and DEF statements:
  5. A DEF* is local to the program unit or subprogram that defines it.
  6. 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.
  7. 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, BASIC signals an error.
  8. DEF* functions can be recursive.
  9. DATA statements in a multiline DEF* are not local to the function; they are local to the program module containing the function definition.
  10. DEF* definitions cannot appear inside a protected region, but they can contain one or more protected regions.
  11. In DEF* functions that contain handlers, the following rules apply:
  12. Only in VAX BASIC can a DEF* function be invoked from within a handler or a DEF function.
  13. In Alpha BASIC, 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.


Examples

Example 1


!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 

Example 2


!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 


DELETE

The DELETE statement removes a record from a relative or indexed file.

Format



Syntax Rules

Chnl-exp is a numeric expression that specifies a channel number associated with a file. It must be immediately preceded by a number sign (#).


Remarks

  1. The DELETE statement removes the current record from a file. Once the record is removed, you cannot access it.
  2. The file specified by chnl-exp must have been opened with ACCESS MODIFY or WRITE.
  3. You can delete a record only if the last I/O statement executed on the specified channel was a successful GET or FIND operation.
  4. The DELETE statement leaves the current record pointer undefined and the next record pointer unchanged.
  5. 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.


Example


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.


DET

The DET function returns the value of the determinant of the last matrix inverted with the MAT INV function.

Format



Syntax Rules

None


Remarks

  1. When a matrix is inverted with the MAT INV statement, BASIC calculates the determinant as a by-product of the inversion process. The DET function retrieves this value.
  2. If your program does not contain a MAT INV statement, the DET function returns a value of zero.
  3. The value returned by the DET function is a floating-point value of the default size.

Example


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 


DIF$

The DIF$ function returns a numeric string whose value is the difference between two numeric strings.

Format



Syntax Rules

Each str-exp can contain up to 60 ASCII digits, an optional decimal point, and an optional leading sign.


Remarks

  1. The DIF$ function does not support E-format notation.
  2. BASIC subtracts str-exp2 from str-exp1 and stores the result in str-var.
  3. The difference between two integers takes the precision of the larger integer.
  4. The difference between two decimal fractions takes the precision of the more precise fraction, unless trailing zeros generate that precision.
  5. The difference between two floating-point numbers takes precision as follows:
  6. BASIC truncates leading and trailing zeros.

Example


PRINT DIF$ ("689","-231") 

Output


920 


Previous Next Contents Index