Saturday, August 11, 2012

AdaTutor - The Format of an Ada Program (1)

Our First Ada Program

with Ada.Text_IO;
procedure Hello is
begin
  Ada.Text_IO.Put_Line("Hello!");
end Hello;
Here's a simple program that displays "Hello!" on the screen.  Ada.Text_IO is a "package" that comes with Ada.  (In Ada 83, the package name is just Text_IO, and for compatibility, Ada 95 also accepts the shorter name.)  We'll learn more about packages later.  Ada.Text_IO contains, among other things, a procedure Put_Line that takes one parameter of type String and displays it.  In this case, the String parameter is "Hello!".  Ada doesn't have any special I/O statements.  I/O is done by calling procedures that Ada provides for us.

Even though the package Ada.Text_IO comes with Ada, our procedure Hello can't "see" it unless it says with Ada.Text_IO;.  With that statement, our procedure can call any procedure or function inside Text_IO. We call a procedure by giving the package name (Ada.Text_IO), followed by a dot and the name of the procedure within the package (Put_Line).  Like C and unlike Fortran, the word CALL isn't used in Ada.

The statement with Ada.Text_IO; is called a context clause because it specifies the "context" in which procedure Hello is compiled.

In Ada a procedure may be called either from another procedure or as the main program.  In this example, Hello is the main program; it calls Put_Line.

Every Ada statement, including the last, ends with a semicolon.  (Technically, the lines above without semicolons aren't statements.)  In Pascal, semicolons come between statements, but in Ada, as in C, a semicolon ends a statement.

In Ada, the end statement may optionally give the name of the procedure or function.  If a name is given, the compiler will check that it agrees with the name at the beginning.  If it doesn't agree, the compiler will issue a message.

The statements between begin and end (this program has only one) are called executable statements; when they're obeyed they're said to be executed.

Local Declarations

with Ada.Text_IO;
procedure Hello is
   I    : Integer;
   X, Y : Float;
begin
  Ada.Text_IO.Put_Line("Hello!");
end Hello;
Local declarations may appear between procedure ... is and begin.  This simple program doesn't need any, but we've added two declarative statements by way of example.  Executable statements are said to be executed when they're obeyed, but when a declarative statement is obeyed, it's said to be elaborated. Declarative statements are elaborated at run time, not compile time.

In this example, when the procedure is called (either from another procedure or as the main program), the declarations are elaborated and variables I, X, and Y come into existence.  Then the executable statement(s) are obeyed.  When the end statement is reached and the procedure returns, I, X, and Y go out of existence.  These three variables are local to procedure Hello and can't be referenced outside this procedure.

If Hello is called again, I, X, and Y will again be brought into existence, but their previous values won't necessarily be remembered between calls.  (Of course, in this simple example, I, X, and Y aren't assigned values anyway.)

Like C and Pascal, and unlike Basic and Fortran, every Ada variable must be declared.  (The index of an Ada for loop declares itself, but we'll discuss that later.)  Unlike Pascal, Ada programs don't need to say "var."  Ada knows that the objects declared above are variables.  Constant declarations contain the reserved word constant and may or may not specify a type, like this:
 Pi     : constant := 3.141592654;
 Two_Pi : constant Float := 2.0 * Pi;
Constant declarations that don't specify a type are called named numbers.

The difference between a named number, like Pi, and a constant declaration that specifies a type, like Two_Pi, is subtle.  We'll explain the difference in the section on More Records and Types under "Universal Types."  For now, it doesn't make any difference whether or not a constant declaration specifies a type.  The compiler can tell, from looking at the constant, whether the type is Integer or Float.

Declarations are elaborated in order, top to bottom.  The two declarations above must appear in the order shown, because the second refers to the first.

Unlike Pascal, Ada allows constant declarations to be intermixed with variable declarations, so long as no declaration refers to a declaration below it.

Question

   1    with Ada.Text_IO;
        procedure Hello is
   2       I    : Integer;
           X, Y : Float;
        begin
   3       Ada.Text_IO.Put_Line("Hello!");
        end Hello;
OK, in the program above, where are the executable statement(s)?


Question

   1    with Ada.Text_IO;
        procedure Hello is
   2       I    : Integer;
           X, Y : Float;
        begin
   3       Ada.Text_IO.Put_Line("Hello!");
        end Hello;
Now, where are the local declaration(s)?

< prev   next >

No comments: