Previous | Next | Table of Contents | Index | Program List | Copyright

2.4 Data Structures: Declaring Constants and Variables

Every program begins with one or more context clauses, followed by a program heading such as

    PROCEDURE Distance IS 
We tell the Ada compiler the names of memory cells used in a program through object (constant and variable) declarations. The programs seen so far contained declarations for constants and variables. The constant declaration
    CM_Per_Inch: CONSTANT Float := 2.54; 
in Program 2.4 specifies that the identifier CM_Per_Inch will be used as the name of the constant 2.54. Identifiers declared in a constant declaration are called constants. Only data values that never change (for example, the number of centimeters per inch is always 2.54) should be associated with an identifier that is a constant. Any Ada statement (other than the declaration) that attempts to change the value of a constant will give rise to a compilation error. The variable declarations
    Initial1: Character;
    Initial2: Character; 
in Program 2.2 give the names of two identifiers that will be used to reference data items that are individual characters as denoted by the predefined identifier Character. The variable declarations
    Inches     : Float;
    Centimeters: Float; 
in Program 2.4 give the names of two identifiers that will be used to reference data items that are floating-point numbers (for example, 30.0 and 562.57) as denoted by the predefined identifier Float. The variable declarations in Program 2.5
    How_Long: Natural;
    How_Fast: Natural;
    How_Far : Natural;
give the names of three identifers whose values will be nonnegative integers, using Ada's predefined integer type Natural. We wish these numbers to be nonnegative because negative time and negative speed do not make good physical sense. We will come back frequently to the question of defining sensible ranges of values for our variables.

An identifier given in a variable declaration statement to the left of the : (colon) symbol is called a variable. Variables are used in a program for storing input data items and computational results. The identifier appearing to the right of the : symbol ( for example, Integer, Float, Character, String) tells the Ada compiler the data type (for example, an integer number, a floating-point number, a single character, or a sequence of characters) of the data that will be stored in the variable. Data types will be considered in more detail in Section 2.10.

You have quite a bit of freedom in selecting the identifiers, or names of variables and constants, that you use in a program. The only restrictions are:

  1. An identifier must always begin with a letter.
  2. An identifier must consist only of letters, digits, and underscores.
  3. You cannot use two or more underscore characters in succession; the first character cannot be an underscore.
  4. You cannot use an Ada reserved word as an identifier.
Some valid and invalid identifiers are listed below.

Valid identifiers:

INITIAL1, initial1, Inches, Centimeters, CM_Per_Inch, hello

Invalid identifiers:

1LETTER, CONSTANT, BEGIN, Two*Four, Joe's, CM__Per__Inch

Note again that both uppercase and lowercase may be used, but remember the style recommendations from Section 2.2. The syntactic rules do not place a limit on the length of an identifier, except that an identifier may not be more than one line long. Ada requires a declaration for every identifier you create and use in your program (no declaration is required or desirable for predefined identifiers). Identifiers that you create and use are called user-defined identifiers.

The names of variables, constants, procedures, packages, package instances, and so on are all identifiers; thus all follow the syntactic rules just given.

The reserved words and identifiers used thus far are shown in Table 2.1 under their appropriate categories.

Table 2.1
Categories of Identifiers in Programs 2.1 through 2.5

Program Names

Hello HelloInitials HelloName InchToCM Distance
Predefined Packages
Ada.Text_IO Ada.Text_IO.Integer_IO Ada.Text_IO.Float_IO
Operations in Predefined Packages
Put New_Line Get
Variables
Initial1 Initial2 First_Name Inches Centimeters How_Long How_Fast  How_Far
Constants
CM_Per_Inch
Predefined Types
Character String Integer Float

In this section we introduced the context clause, program heading, constant declaration, and variable declaration. The syntactic form of each of these Ada language constructs is summarized in the following syntax displays. Each display describes the syntactic form of a construct and provides an example.

SYNTAX DISPLAY
Context Clause

Form:
WITH list of package names;

Example:
WITH Ada.Text_IO;
WITH Ada.Integer_Text_IO;

Interpretation:
A context clause informs the compiler that the named package(s) is (are) being used by this program. The compiler will check all references to resources (e.g., procedures) provided by the package(s), making certain that the program is using them correctly.

Note:
Context clauses can appear only at the very beginning of a source file. Generally we will give only one package name per context clause; this makes it easier to add or delete context clauses.

SYNTAX DISPLAY
Program Heading

Form:
PROCEDURE program name IS

Example:
PROCEDURE Distance IS

Interpretation:
A program heading is used to signal the start of a program.

SYNTAX DISPLAY
Comment

Form:
-- comment

Example:
-- This is a comment

Interpretation:
A double hyphen indicates the start of a comment; the comment ends at the end of the line. Comments are listed with the program but are otherwise ignored by the Ada compiler. Note that if you write a program statement following a comment on the same line, it will be treated by the compiler as part of the comment and therefore it will be ignored!

SYNTAX DISPLAY
Constant Declaration

Form:
Some_constant CONSTANT type := value;

Example:
Pi CONSTANT Float := 3.1459;

Interpretation:
The specified value is associated with the identifier Some_constant. The value of Some_constant cannot be changed by any subsequent program statements.

SYNTAX DISPLAY
Variable Declaration

Form:
variable list : type ;

Example:
Initial1, Initial2: Character; 

Interpretation:
A memory cell is allocated for each variable (an identifier) in the variable list. The type of data (Character in this case) to be stored in each variable is specified between the colon and the semicolon. Commas are used to separate the identifiers in the variable list.

To make it easier to add and delete variable declarations, we generally will write each declaration on its own line and give only one variable per declaration.

PROGRAM STYLE
Choosing Identifier Names

It is very important to pick meaningful names for identifiers; they will be easier to understand when used in a program. For example, the identifier Salary would be a good name for a variable used to store a person's salary; the identifiers S and Bagel would be bad choices. There is no restriction on the length of an identifier. However, it is difficult to form meaningful names using fewer than three letters.

On the other hand, typing errors become more likely when identifiers are too long. A reasonable rule of thumb is to use names that are between three and ten characters in length. If you mistype an identifier, the compiler will usually detect this as a syntax error and display an undefined identifier message during program translation. Sometimes mistyped identifiers resemble other identifiers, so avoid picking names that are similar to each other. Make sure that you do not choose two names that are identical except for their use of case; the compiler will not be able to distinguish between them.

PROGRAM STYLE
Form of Declarations and Context Clauses

From the syntax displays, you can see that Ada permits several package names to appear in a single context clause and several variable names to appear in a declaration. Declarations are often changed during the development of a program as variables are added and removed. It is therefore much easier to develop a program (and to read it as well) if each variable and constant is declared in a separate declaration on its own line. All programs in this book follow this style convention, and we recommend that you follow it too.

The same recommendation applies to context clauses: Because any number of context clauses can precede a program, we recommend that each context clause name only a single package and appear on its own line.

PROGRAM STYLE
Banner Comments

Each program in this book (with the exception of the first few in this chapter) contains a banner comment, sometimes called a block comment or header comment, giving a brief description of the program, with author and date information. An example is
------------------------------------------------------------------------------
--| Finds distance traveled, given travel time and average speed  
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------------
Like all comments, banner comments are ignored by the compiler and are inserted purely for documentation purposes. The use of banner comments is strongly recommended.

Exercises for Section 2.4

Self-Check

  1. Should the value of pi (3.14159) be stored in a constant or a variable? Why?
  2. Which of these are valid Ada identifiers?
    	MyProgram	prog2	Prog#2	2NDone	procedure	"MaxScores"
    
  3. Indicate which of the identifiers below are Ada reserved words, predefined identifiers, identifiers, and invalid identifiers.
    	END	Put		BILL		PROCEDURE	SUE'S
    
    	Rate	OPERATE	START		BEGIN		CONSTANT
    
    	XYZ123	123XYZ	This__Is__A__Long__One	Y=Z
    

Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.