Declaring variables to a CL program or procedure

All variables must be declared (defined) to the CL program or procedure before they can be used by the program or procedure.

There are two ways of declaring variables:

  • Declare variable. Defining it is accomplished using the Declare CL Variable (DCL) command and consists of defining the attributes of the variable. These attributes include type, length, and initial value.
    DCL  VAR(&AREA)  TYPE(*CHAR)  LEN(4)  VALUE(BOOK)
  • Declare file. If your CL program or procedure uses a file, you must specify the name of the file in the FILE parameter on the Declare File (DCLF) command. The file contains a description (format) of the records in the file and the fields in the records. During compilation, the DCLF command implicitly declares CL variables for the fields and indicators defined in the file.

    For example, if the DDS for the file has one record in it with two fields (F1 and F2), then two variables, &F1 and &F2, are automatically declared in the program.

    DCLF  FILE(MCGANN/GUIDE)

    If the file is a physical file which was created without DDS, one variable is declared for the entire record. The variable has the same name as the file, and its length is the same as the record length of the file.

The declare commands must precede all other commands in the program or procedure (except the PGM command), but they can be intermixed in any order.

Rules for using the Declare CL Variable command

In its simplest form, the Declare CL Variable (DCL) command has the following parameters.

Declare CL Variable (DCL) command has these parameters

When you use a DCL command, you must use the following rules:

  • The CL variable name must begin with an ampersand (&) followed by as many as 10 characters. The first character following the & must be alphabetic and the remaining characters alphanumeric. For example, &PART.
  • The CL variable value must be one of the following:
    • A character string as long as 5000 characters.
    • A packed decimal value totaling up to 15 digits with as many as 9 decimal positions.
    • A logical value '0' or '1', where '0' can mean off, false, or no, and '1' can mean on, true, or yes. A logical variable must be either '0' or '1'.
    • Start of changeAn integer value of two, four, or eight bytes. The value can be negative if *INT is specified for the TYPE parameter. The value must be positive or zero if *UINT is specified for the TYPE parameter. LEN(8) can be specified only if the CL source is compiled with the Create CL Module (CRTCLMOD) command or the Create Bound CL Program (CRTBNDCL) command.End of change
    • A pointer value which can hold the location of data in storage.
  • If you do not specify an initial value, the following is assumed:
    • '0' for decimal variables.
    • Blanks for character variables.
    • '0' for logical variables.
    • '0' for integer variables.
    • Null for pointer variables.

    For decimal and character types, if you specify an initial value and do not specify the LEN parameter, the default length is the same as the length of the initial value. For type *CHAR, if you do not specify the LEN parameter, the string can be as long as 5000 characters. For type *INT or *UINT, if you do not specify the LEN parameter, the default length is 4.

  • Declare the parameters as variables in the program DCL statements.