Named constants

Named constants can be declared for scalars or structures.

As a scalar identifier, a named constant is declared with the VALUE attribute along with other data attributes. All references to the name are logically treated as a reference to the appropriate constant but with the complete set of attributes, whether explicitly declared or defaulted.

A structure can define a namespace of named constants, when the VALUE attribute is specified on the leaf elements, and the references to the members of all structure elements are unambiguous. You can specify the elements of a structure with the VALUE attribute, provided that the structure meets all of the following conditions:

Note: The effect of the use of a named constant might not be exactly the same as the use of an unnamed constant. The attributes for a named constant are taken from the declaration which includes explicit and default attributes. The attributes for an unnamed constant are deduced from the shape, form, and size of the constant. For string data, if the length is not specified, or is specified with an asterisk, the length is determined from the length of the restricted expression.

Named constants can be more precise to use in an application program, and they can offer more predictable results. For example, if the named constant Unit is defined as FIXED BINARY VALUE(1), it has the attributes FIXED BINARY(15) VALUE(1). If you simply use the digit 1, its attributes are FIXED DECIMAL(1,0). See Figure 1 for other differences that can occur.

In addition, named constants allow you to parameterize your application, which makes it easier to debug and maintain.

Named constants can be declared for arithmetic data, string data, and for pointers and offsets. A named constant must be declared before it is used.