[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP BASIC for OpenVMS
Reference Manual


Previous Contents Index


DATA

The DATA statement creates a data block for the READ statement.

Format



Syntax Rules

  • Num-lit specifies a numeric literal.
  • Str-lit is a character string that starts and ends with double or single quotation marks. The quotation marks must match.
  • Unq-str is a character sequence that does not start or end with double quotation marks and does not contain a comma.
  • Commas separate data elements. If a comma is part of a data item, the entire item must be enclosed in quotation marks.

Remarks

  • Because HP BASIC treats comment fields in DATA statements as part of the DATA sequence, you should not include comments.
  • A DATA statement must be the last or the only statement on a physical line.
  • DATA statements must end with a line terminator.
  • When a DATA statement is continued with an ampersand (&), HP BASIC interprets all characters between the keyword DATA and the ampersand as part of the data. Any code that appears on a noncontinued line is considered a new statement.
  • You cannot use the percent sign suffix for integer constants that appear in DATA statements. An attempt to do so causes HP BASIC to signal the error, "Data format error" (ERR=50).
  • DATA statements are local to a program module.
  • HP BASIC does not execute DATA statements. Instead, control is passed to the next executable statement.
  • A program can have more than one DATA statement. HP BASIC assembles data from all DATA statements in a single program unit into a lexically ordered single data block.
  • HP BASIC ignores leading and trailing blanks and tabs unless they are in a string literal.
  • Commas are the only valid data delimiters. You must use a quoted string literal if a comma is to be part of a string.
  • HP BASIC ignores DATA statements without an accompanying READ statement.
  • HP BASIC signals the error "Data format error" if the DATA item does not match the data type of the variable specified in the READ statement or if a data element that is to be read into an integer variable ends with a percent sign (%). If a string data element ends with a dollar sign ($), HP BASIC treats the dollar sign as part of the string.

Example


10 DECLARE INTEGER A,B,C
   READ A,B,C
   DATA 1,2,3
   PRINT A + B + C

Output


 6

DATE$

The DATE$ function returns a string containing a day, month, and year in the form dd-mmm-yy.

Format

str-var = DATE$ (int-exp)


Syntax Rules

  • Int-exp can have up to 6 digits in the form yyyddd, where the characters yyy specify the number of years since 1970 and the characters ddd specify the day of that year. The day of year must be a value between 1 and the number of days in the specified year.
  • You must fill all three of the d positions with digits or zeros before you can fill the y positions. For example:
    • DATE$(121) returns the date 01--May--70, day 121 of the year 1970.
    • DATE$(1201) returns the date 20--Jul--71, day 201 of the year 1971.
    • DATE$(12001) returns the date 01--Jan--82, day one of the year 1982.
    • DATE$(10202) returns the date 20--Jul--80, day 202 of the year 1980.

Remarks

  • If int-exp equals zero, DATE$ returns the current date.
  • The str-var returned by the DATE$ function consists of nine characters and expresses the day, month, and year in the form dd-mmm-yy.
  • If you specify an invalid date, such as day 385, results are unpredictable.
  • If you specify a floating-point expression for int-exp, HP BASIC truncates it to an integer of the default size.

Example


DECLARE STRING todays_date
todays_date = DATE$(0)
PRINT todays_date

Output


09-Oct-99

The DATE4$ function is strongly recommended as replacement for the DATE$ function to avoid problems in the year 2000 and beyond. It functions the same as the DATE$ function except that the year portion of the result string contains two more digits indicating the century. For example:


PRINT 32150, DATE$ (32150), DATE4$ (32150)

This produces the following output:


32150   30-May-02   30-May-2002

DATE4$

The DATE4$ function returns a string containing a day, month, and year in the form dd-mmm-yyyy.

Format

str-var = DATE4$ (int-exp)


Syntax Rules

  • Int-exp can have up to 6 digits in the form yyyddd, where the characters yyy specify the number of years since 1970 and the characters ddd specify the day of that year. The day of year must be a value between 1 and the number of days in the specified year.
  • You must fill all three of the d positions with digits or zeros before you can fill the y positions.

Remarks

The DATE4$ function is strongly recommended as replacement for the DATE$ function to avoid problems in the year 2000 and beyond. It functions the same as the DATE$ function except that the year portion of the result string contains two more digits indicating the century. For example:


PRINT 32150, DATE$ (32150), DATE4$ (32150)

Produces the following output:


32150   30-May-02   30-May-2002

See the description of the DATE$ function for more information.


DECIMAL

The DECIMAL function converts a numeric expression or numeric string to the DECIMAL data type.

Format

decimal-var = DECIMAL (exp [, int-const1, int-const2 ] )


Syntax Rules

  • Int-const1 specifies the total number of digits (the precision) and int-const2 specifies the number of digits to the right of the decimal point (the scale). If you do not specify these values, HP BASIC uses the d (digits) and s (scale) defaults for the DECIMAL data type.
  • Int-const1 and int-const2 must be positive integers from 1 to 31. Int-const2 cannot exceed the value of int-const1.
  • Exp can be either numeric or numeric string. If a numeric string, it can contain the ASCII digits 0 to 9, a plus sign (+), a minus sign (--), and a period (.).

Remarks

  • If exp is a string, HP BASIC ignores leading and trailing spaces and tabs.
  • The DECIMAL function returns a zero when a string argument contains only spaces and tabs, or when it is null.

Example


DECLARE STRING CONSTANT format_string = "##.###"
DECLARE STRING num_value, DECIMAL(5,3) B
INPUT "Enter a numeric value";num_value
B = DECIMAL(num_value,5,3)
PRINT USING format_string, B

Output


Enter a numeric value? 6
 6.000

DECLARE

The DECLARE statement explicitly assigns a name and a data type to a variable, an entire array, a function, or a constant.

Format



Syntax Rules

  • Data-type can be any HP BASIC data type keyword or a data type defined by a RECORD statement. Data type keywords, size, range, and precision are listed in Table 1-2.
  • Variables
    • Decl-item names an array, a record, or a variable.
    • A decl-item named in a DECLARE statement cannot be named in another DECLARE statement, or in a DEF, EXTERNAL, FUNCTION, SUB, COMMON, MAP, DIM, HANDLER, or PICTURE statement.
    • Each decl-item is associated with the preceding data type. A data type is required for the first decl-item.
    • Decl-items of data type STRING are dynamic strings.
    • When you declare an array, HP BASIC allows you to specify both lower and upper bounds for each dimension of the array. The upper bounds is required; the lower bounds is optional.
      • Int-const1 specifies the lower bounds of the array.
      • Int-const2 specifies the upper bounds of the array and, when accompanied by int-const1, must be preceded by the keyword TO.
      • Int-const1 must be less than or equal to int-const2.
      • If you do not specify int-const1, HP BASIC uses zero as the default lower bounds.
      • Int-const1 and int-const2 can be any combination of negative or positive values or zero.
  • DEF Functions
    • Def-name names the DEF function.
    • Data-type specifies the data type of the value the function returns.
    • Def-params specify the number and, optionally, the data type of the DEF parameters. Parameters define the arguments the DEF expects to receive when invoked.
      • When you specify a data type, all following parameters are of that data type until you specify a new data type.
      • If you do not specify any data type, parameters take the current default data type and size.
      • The number of parameters equals the number of commas plus 1. For example, empty parentheses specify one parameter of the default type and size; one comma inside the parentheses specifies two parameters of the default type and size; and so on. One data type inside the parentheses specifies one parameter of the specified data type; two data types separated by one comma specifies two parameters of the specified type, and so on.
  • Named Constants
    • Const-name is the name you assign to the constant.
    • Data-type specifies the data type of the constant. The value of the const must be numeric if the data type is numeric and string if the data type is STRING. If the data type is STRING, const must be a quoted string or another string constant.
    • Const-exp cannot be a data type that was defined with the RECORD statement.
    • Data-type cannot be a data type defined by a record statment.
    • String constants cannot exceed 498 characters.
    • HP BASIC allows const-exp to be an expression for all data types except DECIMAL. Expressions are not allowed as values when you name DECIMAL constants.
    • Allowable operators in DECLARE CONSTANT expressions include all valid arithmetic, relational, and logical operators except exponentiation. Built-in functions cannot be used in DECLARE CONSTANT expressions. The following examples use valid expressions as values:


      DECLARE DOUBLE CONSTANT max_value = (PI/2)
      DECLARE STRING CONSTANT left_arrow = "<-----" + LF + CR
      

Remarks

  • The DECLARE statement is not executable.
  • The DECLARE statement must lexically precede any reference to the variables, functions, or constants named in it.
  • To declare a virtual or run-time array, use the DIMENSION statement.
  • Variables
    • Subsequent decl-items are associated with the specified data type until you specify another data type.
    • All variables named in a DECLARE statement are initialized to zero if numeric or to the null string if string.
  • DEF Functions
    • The DECLARE FUNCTION statement allows you to name a function defined in a DEF or DEF* statement, specify the data type of the value the function returns, and declare the number and data type of the parameters.
    • Data type keywords must be separated by commas.
    • The first specification of a data type for a def-param is the default for subsequent arguments until you specify another def-param. For example:


      DECLARE DOUBLE FUNCTION interest(DOUBLE,SINGLE,,)
      

      This example declares two parameters of the default type and size, one DOUBLE parameter, and three SINGLE parameters for the function named interest.
  • Named Constants
    • The DECLARE CONSTANT statement allows you to name a constant value and assign a data type to that value. Note that you can specify only one data type in a DECLARE CONSTANT statement. To declare a constant of another data type, you must use a second DECLARE CONSTANT statement.
    • During program execution, you cannot change the value assigned to the constant.
    • The specified data-type determines the data type of the constant. For example:


      DECLARE LONG CONSTANT True = -1, False = 0
      DECLARE REAL CONSTANT ZZZ = 123.0
      DECLARE BYTE CONSTANT YYY = '123'L
      PRINT True, False, ZZZ, YYY
      

      Output


      -1            0            123            123
      

      In this example, HP 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

  • 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:
    • 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 (.).
  • 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.
  • 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 DEF parameters. Because the parameters are local to the DEF function, any reference to these variables outside the DEF body creates a different variable.
  • 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.
  • You can specify up to 255 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 (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

  • When HP BASIC encounters a DEF statement, control of the program passes to the next executable statement after the DEF.
  • The function is invoked when you use the function name in an expression.
  • You cannot specify how parameters are passed. When you invoke a 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 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.
  • A DEF function is local to the program, subprogram, function, or picture 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 your program invokes a function with a name that does not start with FN before the DEF statement defines the function, HP BASIC signals an error.
  • 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.
  • DATA statements in a multiline DEF are not local to the function; they are local to the program module containing the function definition.
  • The function value is initialized to zero or the null string each time you invoke the function.
  • DEF definitions cannot appear inside a protected region. However, DEF can contain one or more protected regions.
  • DEF functions can be invoked within handlers, within DEF functions, and within DEF* functions.
  • 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.
  • If an exception is not handled within a DEF function, control is transferred to the module that invoked the DEF function.
  • ON ERROR statements within a DEF function are local to the function.
  • 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.
  • You cannot transfer control into a multiline DEF except by invoking the function.
  • DEF functions can be recursive. However, HP 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


Previous Next Contents Index