1.2 Second Example: Show_Date

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

 

Im1-2b.gif (3703 bytes) The architecture of our second example includes two packages from Ada’s predefined environment. The main procedure, named Show_Date, uses resources exported by the two packages. Thus, the procedure has a dependency relationship with the two packages. The first package exports the Put_Line procedure, while the second exports four functions used by Show_Date.

As in the first example diagram, the dashed arrows represent dependency clauses and can be clicked to jump to the corresponding lines of code. The code that defines the full interface to package Ada.Calendar is given in Appendix B. (This package exports more than just the four functions shown in the diagram, and the Ada.Text_IO package exports more than just the Put_Line procedure.)

Notes about the example procedure

The declarative part declares four variables, using type definitions (Time, Year_Number, Month_Number, and Day_Number) exported by the predefined package, Ada.Calendar. In reading the first line, you might say to yourself: "The_Time is of type Time, provided by Ada.Calendar." The scope of each of these variables (the region of code over which the item has effect) begins at the point of declaration and ends at the end of the unit in which it is declared.

The executable part begins with four assignment statements that make use of four functions (Clock, Year, Month and Day) exported by the predefined package, Ada.Calendar. Then there are four procedure calls, all calling the Put_Line procedure exported by the predefined package Ada.Text_IO. The last three procedure calls make use of an attribute of Ada called Image. (Attributes are covered in Chapter 4 and listed in Appendix A.)

Note that declarations introduce new identifiers while statements are executed, and that declarations appear in the declarative part while statements appear in the executable part of a program unit. During run-time the elaboration of declarations occurs first, followed by the execution of statements. Elaboration is the process by which the declared item comes into existence and then (in some cases) is assigned an initial value.

Source Code Listing

----------------------------------------------------------
--  This program finds the current year, month and day  
--  using the facilities of Ada.Calendar, and then 
--  displays the found values using the facilities of 
--  Ada.Text_IO.
----------------------------------------------------------
with Ada.Text_IO;                 -- context clauses
with Ada.Calendar;
procedure Show_Date is
                                  -- declarative part
  The_Time  : Ada.Calendar.Time;
  The_Year  : Ada.Calendar.Year_Number;
  The_Month : Ada.Calendar.Month_Number;
  The_Day   : Ada.Calendar.Day_Number;

begin                             -- executable part

  The_Time  := Ada.Calendar.Clock;
  The_Year  := Ada.Calendar.Year(The_Time);
  The_Month := Ada.Calendar.Month(The_Time);
  The_Day   := Ada.Calendar.Day(The_Time);

  Ada.Text_IO.Put_Line("Today's Date:");
  Ada.Text_IO.Put_Line("  Year = " & 
            Ada.Calendar.Year_Number'Image(The_Year));
  Ada.Text_IO.Put_Line("  Month = " & 
            Ada.Calendar.Month_Number'Image(The_Month));
  Ada.Text_IO.Put_Line("  Day = " & 
            Ada.Calendar.Day_Number'Image(The_Day));

end Show_Date; 
----------------------------------------------------------

If you compile and link the above program and then run it on say, October 24, 2001, the following lines of text should be displayed:

     Today's Date:
       Year = 2001
       Month = 10
       Day = 24

The above code is rather verbose, because of the repetitious inclusion of the names of the predefined packages, Ada.Calendar and Ada.Text_IO, as prefixes. The next page describes two ways of reducing such verbosity.

Related Topics

2.4 Procedures 2.5 Functions
4.0 Types and Attributes A.3 List of Attributes
B.2 Package Ada and Children

[ Back to top of pagePrev ] Next ]