Section 1.2 - Simple Program

Here's a simple program in Ada that simply prints a message (this is often called the hello, world! program):

-- Print a simple message to demonstrate a trivial Ada program.
with Ada.Text_IO;
procedure Hello is
begin
 Ada.Text_IO.Put_Line("Hello, world!");
end Hello;

Here's an explanation of each line:

  1. The first line illustrates a comment; Ada comments begin with ``--'' and end at the end of the line (C++ comments that begin with // work the same way).
  2. The second line illustrates a with clause, which specifies the library units (other items) that we need. This particular with clause specifies that we need Ada.Text_IO. Ada.Text_IO is a predefined library unit that provides operations to perform basic text input and output.
  3. The third line states that we're defining a new procedure named Hello. Note that in Ada there's nothing special about the name of the main program (in C and C++, it must be called main, and in Pascal, it must be specially identified as the program).
  4. The fourth line just has the keyword begin, which begins the definition of the procedure Hello.
  5. The fifth line calls Ada.Text_IO's procedure Put_Line, which prints a line to the current output (usually the screen) and then ends the current line. The basic syntax for calling a procedure is to give the library unit name, a period, the name of the procedure, and then list the parameters (if any) enclosed in parentheses (we'll see how to simplify this soon). In Ada, strings are surrounded by double quotes (the same as C and C++; Pascal uses single quotes).
  6. The last line ends the definition of the procedure.

Ada terminates each statement with a semicolon. This is like C and C++ and unlike standard Pascal (which uses semicolons as statement separators).

Just a quick note - if you're using an Ada 83 compiler instead of an Ada 95 compiler, please see the note about using Ada 83 before compiling this program.


Quiz:


What is the name of the new procedure defined above?
  1. Text_IO
  2. Hello
  3. Put_Line

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to the outline of lesson 1

David A. Wheeler (wheeler@ida.org)