Every program begins with one or more context clauses, followed by a program heading such as
PROCEDURE Distance ISWe 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:
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 DistancePredefined Packages
Ada.Text_IO Ada.Text_IO.Integer_IO Ada.Text_IO.Float_IOOperations in Predefined Packages
Put New_Line GetVariables
Initial1 Initial2 First_Name Inches Centimeters How_Long How_Fast How_FarConstants
CM_Per_InchPredefined 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
WITH
list of package names;
WITH Ada.Text_IO; WITH Ada.Integer_Text_IO;
SYNTAX DISPLAY
PROCEDURE
program name IS
PROCEDURE Distance IS
SYNTAX
DISPLAY
--
comment
-- This is a comment
SYNTAX DISPLAY
Constant Declaration
CONSTANT
type :=
value;
Pi CONSTANT Float := 3.1459;
SYNTAX DISPLAY
Variable Declaration
Initial1, Initial2: Character;
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
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
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
------------------------------------------------------------------------------ --| Finds distance traveled, given travel time and average speed --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------------
MyProgram prog2 Prog#2 2NDone procedure "MaxScores"
END Put BILL PROCEDURE SUE'S Rate OPERATE START BEGIN CONSTANT XYZ123 123XYZ This__Is__A__Long__One Y=Z
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.