[Ada Information Clearinghouse]
Ada '83 Rationale, Sec 3.1: Simple Declarations: Variables and Constants

"Rationale for the Design of the
Ada® Programming Language"

[Ada '83 Rationale, HTML Version]

Copyright ©1986 owned by the United States Government. All rights reserved.
Direct inquiries to the Ada Information Clearinghouse at adainfo@sw-eng.falls-church.va.us.

CHAPTER 3: Classical Programming

3.1 Simple Declarations: Variables and Constants

Variables and constants are declared by the simplest kind of declaration, called an object declaration; the general form is similar to that used in Pascal but there are some differences.

A delicate balance is necessary in the handling of variables and constants. Reliability and security demand that the two concepts be clearly separated. On the other hand it is convenient if a variable declaration can be changed into a constant declaration by a very simple alteration of the program text. Ada meets these two goals by having two distinct but related forms of declaration for the two concepts. Thus the following object declaration:

    C :  constant  REAL :=  300_000.0;

declares a constant, whereas the following object declaration declares a variable with an initial value:

    SPEED :  REAL :=  C/1500.0;

The adjective constant which appears in the declaration of a constant states that the value of this object must not be altered after its initialization. By writing this adjective, the programmer expresses his intent in an explicit manner and requests the assistance of the compiler in forbidding attempts to alter the value - whether by accident, by mistake, or deliberately.

Following Algol 68 however, the adjective constant does not mean that the initial value must be known at compilation time (that is, in Ada terminology, the initial value expression need not be static).

For example, a function to determine whether a given integer number is prime may declare an upper bound on divisors:

function PRIME(X :  INTEGER) return BOOLEAN  is
end PRIME;

and clearly the value of this upperbound, although constant for a given call of the function, will be computed in terms of the actual parameter of the function, and hence dynamically.

Good programming style in Ada will systematically emphasize constants, even for short texts where the danger of accidental alterations would be very slight. For example, an Ada formulation of the classical procedure to exchange the values of two variables will emphasize the fact that the temporary variable - OLD_LEFT - is indeed a constant:

procedure EXCHANGE(LEFT, RIGHT :  in out INTEGER) is
  OLD_LEFT :  constant INTEGER  :=  LEFT;
  LEFT    :=  RIGHT;

An initialization is required in the case of a constant. For variables it is allowed but not required. Allowing initialization of a variable in its declaration enables one to ensure that the initial state of variables is well-defined. Furthermore, this avoids long sequences of initialization assignment statements, which are divorced from the declarations and hence hard to locate.

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.