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

2.2 The "Look" of Ada Programs

Before beginning our study of programming with Ada, we will examine three short programs. Don't worry about understanding the details of these programs yet; it is the general "look"of the programs that is of concern now.

Example 2.1

Program 2.1 is the Ada program introduced at the end of Chapter 1. This time the lines are numbered for convenience in explaining them to you; when you write a program, you do not number the lines.

Program 2.1
A First Ada Program, with Sample Output

     1  WITH Ada.Text_IO;
     2  PROCEDURE Hello IS
     3     
     4  BEGIN -- Hello
     5    
     6    Ada.Text_IO.Put (Item => "Hello there. ");
     7    Ada.Text_IO.Put (Item => "We hope you enjoy studying Ada!");
     8    Ada.Text_IO.New_Line;
     9    
    10  END Hello;
Sample Run
Hello there. We hope you enjoy studying Ada!

Line 1 informs the compiler that this program will be making use of a package called Ada.Text_IO. A statement like this almost always precedes the rest of an Ada program file. A fuller explanation of packages will appear in the next section; for now you should know that input and output are done in Ada by means of standard packages. Ada.Text_IO is the most used standard package.

Line 2 informs the compiler that this program is to be called Hello (in Ada a program is called a PROCEDURE). Line 3 is left blank just to make the program easier to read. The section of the program between BEGIN (line 4) and END (line 10), called the body, or executable statements section, contains a list of actions the program is to perform. Each statement or action ends with a semicolon. In this program there are three statements, all calling for output actions. The statements in lines 6 and 7,

    Ada.Text_IO.Put(Item => "Hello there. ");
    Ada.Text_IO.Put(Item => "We hope you enjoy studying Ada!");
display the strings enclosed in quotes on the screen. The statement in line 8,
    Ada.Text_IO.New_Line; 
terminates the line displayed on the screen by advancing the cursor (a blinking place marker) to the first position of the next line. All these statements are prefixed by Ada.Text_IO as our way of indicating to the compiler (and to the reader of this program) that the operations in question are meant to be the ones provided by the Ada.Text_IO package. More about this later.

Example 2.2

Program2.2 is similar to the last one, but with the important difference that the program asks the user (the person running the program) to enter his or her initials, then greets the user with these initials.

Program 2.2
Displaying Initials

  1  WITH Ada.Text_IO;
  2  PROCEDURE Hello_Initials IS
  3     
  4    Initial1 : Character;
  5    Initial2 : Character;
  6     
  7  BEGIN -- Hello_Initials
  8     
  9    Ada.Text_IO.Put (Item => "Enter your two initials> ");
 10    Ada.Text_IO.Get (Item => Initial1);  
 11    Ada.Text_IO.Get (Item => Initial2);
 12    Ada.Text_IO.New_Line;
 13     
 14    Ada.Text_IO.Put (Item => "Hello ");
 15    Ada.Text_IO.Put (Item => Initial1);  
 16    Ada.Text_IO.Put (Item => Initial2);
 17    Ada.Text_IO.Put (Item => ".  We hope you enjoy studying Ada!");
 18    Ada.Text_IO.New_Line;
 19     
 20  END Hello_Initials;
Sample Run
Enter your two initials> MF

Hello MF.  We hope you enjoy studying Ada!
Lines 4 and 5 identify the names of two memory cells (Initial1 and Initial2) that will be used to store each initial. The section of the program between the reserved word IS (line 2) and the reserved word BEGIN (line 7) is called the declarative section, or sometimes just the declarations. Generally this section describes objects (such as our two memory cells) and types (more on this later) to the compiler. If there are no declarations, as in the first program, this section will be empty.

The statements in lines 9 through 12 are all calls to input/output procedures. As before, each statement containing a Ada.Text_IO.Put causes some information to be displayed on the video screen during program execution. The statement

    Ada.Text_IO.Put(Item => "Enter your two initials> ");
asks the program user to enter two letters. The statements
    Ada.Text_IO.Get(Item => Initial1); 
    Ada.Text_IO.Get(Item => Initial2); 
cause the program to wait until two letters are entered on the keyboard by the program user. These letters are "read" (stored) into the two memory cells listed, one letter per cell. The last output line of the program is displayed by the Ada.Text_IO.Put statements in lines 14 through 17. These statements display the string "Hello ", the two letters just read, and finally the longer greeting message.

Example 2.3

Program 2.3 is similar to Program 2.2, except that it reads a person's name instead of just that person's initials. The declaration in line 4 describes a location First_Name able to hold a sequence of exactly ten characters (letters, digits, etc.). That is why the prompt in lines 8 through 10 requests an entry of exactly that many letters.

Program 2.3
Displaying the User's Name

 1  WITH Ada.Text_IO;
 2  PROCEDURE Hello_Name IS
 3   
 4    First_Name: String(1..10);
 5   
 6  BEGIN  -- Hello_Name
 7     
 8    Ada.Text_IO.Put (Item => "Enter first name, exactly 10 letters.");
 9    Ada.Text_IO.New_Line;
10    Ada.Text_IO.Put (Item => "Add spaces at end if it's shorter.> ");
11    Ada.Text_IO.Get (Item => First_Name);
12    Ada.Text_IO.New_Line;
13     
14    Ada.Text_IO.Put (Item => "Hello ");
15    Ada.Text_IO.Put (Item => First_Name);  
16    Ada.Text_IO.Put (Item => ".  We hope you enjoy studying Ada!");
17    Ada.Text_IO.New_Line;
18     
19  END Hello_Name;
Sample Run
Enter first name, exactly 10 letters.
Add spaces at end if it's shorter.> Michael   

Hello Michael   .  We hope you enjoy studying Ada!

One of the nice things about Ada is that it lets us write program statements that resemble human language or everyday mathematics. At this point, you probably can read and understand the sample programs, even though you may not know how to write your own programs. In the following sections you will learn more details about the Ada programs we have looked at so far.

Reserved Words and Identifiers

All of the lines in the preceding programs satisfy the syntax rules for the Ada language. The programs contain several different elements: reserved words, predefined identifiers, special symbols, and names for memory cells. Let's look at the first three categories. The reserved words all appear in uppercase; they have special meanings in Ada and cannot be used for other purposes. The reserved words in Programs 2.1 through 2.3 are (in order of appearance)

    WITH  PROCEDURE  IS  BEGIN  END
The predefined identifiers also have special meanings, but they can be used by the programmer for other purposes (however, we don't recommend this practice). The predefined identifiers in Programs 2.1 through 2.3 are (in order of appearance)
    Ada.Text_IO  Put  New_Line  Character  Get  String 
There are also some symbols (e.g., =, *, >=) that have special meanings in Ada. Appendix A contains a complete list of reserved words and special symbols; Appendix C summarizes the predefined identifiers.

What is the difference between reserved words and predefined identifiers? You cannot use a reserved word as the name of a memory cell, but in certain cases you can use a predefined identifier. Exactly how Ada would treat such a "reused" predefined identifier depends on just which identifier is involved. In any case, the result would be very confusing to the reader of the program. Therefore we strongly recommend that you treat predefined identifiers as though they were reserved words and refrain from reusing them.

The other identifiers appearing in the three sample programs are described in more detail in the next sections.

PROGRAM STYLE
Use of Uppercase and Lowercase

Throughout the text, issues of good programming style are discussed in displays like this one. Programming style displays provide guidelines for improving the appearance and the readability of your programs. Most programs are examined, studied, and used by someone other than the original author. A program that follows consistent style conventions is easier to read and understand than one that is sloppy or inconsistent. These conventions have no effect whatsoever on the computer; they just make it much easier for humans to understand programs. In this text, reserved words always appear in uppercase. This is because the reserved words determine the structure and organization of the program. Writing them in uppercase, combined with a consistent indentation style, makes the structure and organization of the program immediately visible to the human eye. Identifiers are in mixed uppercase and lowercase. The first letter of each identifier is capitalized. If an identifier consists of two or more words (such as New_Line), each word is usually capitalized and the words are sometimes separated by an underscore character. The compiler does not differentiate between uppercase and lowercase in reading your program. You could write the reserved word BEGIN as begin and the predefined identifier Character as CHARACTER or even ChArAcTeR. The compiler doesn't care, but we do as humans striving for clarity and consistency. The compiler does, however, treat the underscore as a character, so Two_Words is different from TwoWords. Your instructor may prefer a different convention; if so, it is prudent to follow it. In the end, what matters most is using a well thought out and consistent programming style.

Programs and Packages

Ada is a language designed for writing real-world programs that can be very large, sometimes numbering hundreds of thousands of statements. Because a single program file of that length would be completely unmanageable for humans and computers alike, Ada is built around the idea of libraries and packages. Using these, sets of commonly used operations can be tested once and then put in a library for others to use. Ada comes with several standard, predefined packages; the first one you have seen is Ada.Text_IO. All the predefined Ada libraries begin with the form Ada.[1] Later in the book you will learn how to use other predefined packages and to write packages of your own. For now, keep in mind that almost every Ada program is preceded by at least one WITH clause (formally called a context clause) of the form

    WITH Package_Name;
WITH clauses inform the compiler to which packages it must refer in order to understand the operations you are using. Preceding a program by the context clause
    WITH Ada.Text_IO; 
informs the compiler that the program will be using this package to read input data from the keyboard and display output data on the monitor. Omitting the context clause would cause one or more compilation errors.


[1] Readers familiar with Ada 83 will note that this is a change in the system of naming the standard libraries. To maintain compatibility with Ada 83 programs, the Ada 95 standard allows Ada 83 standard packages to be called by their original names. This book consistently uses the Ada 95 names.
Previous | Next | Table of Contents | Index | Program List | Copyright

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