# 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:
   - A function data type is required.
   - 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 (0 to 9), dollar signs ($), underscores (_), or periods (.).
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:
   - The function data type is optional.
   - The first character of the def-name must be an alphabetic letter (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.
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:
   - 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.
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
    - Statements specifies the operations the function performs.
    - The END DEF or FNEND statement is required to end a multiline DEF.
    - BASIC does not allow you to specify any statements that indicate the beginning or end of any SUB, FUNCTION, PICTURE, HANDLER (attached handlers are legal), PROGRAM or DEF in a function definition.
    - Exp specifies the function result. Exp must be compatible with the DEF data type.

## 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:
    - 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.
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:
   - A function data type is required.
   - 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 (0 to 9), dollar signs ($), underscores (_), or periods (.).
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:
   - 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.
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:
   - 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.
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*
    - Statements specifies the operations the function performs.
    - The END DEF or FNEND statement is required to end a multiline DEF*.
    - 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.

## 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:
   - 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.
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:
    - 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.
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:
   - 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.
6. BASIC truncates leading and trailing zeros.

## Example

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

Output

    920

Previous | Next | Contents | Index