Section 1.2 - Simple Program
Here's a simple program in Ada that simply prints a message,
often called the hello, world!
program (for the moment, ignore the buttons below the sample program):
Here's an explanation of each line:
-
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).
-
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.
The library unit Ada.Text_IO is a predefined
library unit that provides operations to perform
basic text input and output.
-
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).
-
The fourth line just has the keyword begin, which begins
the definition of the procedure Hello.
-
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).
-
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).
Unless you're using a very old browser you should see a button labelled
"Compile with WebAda" below the source code.
If you have a working connection to the Internet, just press that button
and you'll compile the Ada code with the
WebAda
system.
You can also make changes and see the effect of those changes.
There are a few other buttons you may find useful:
the "reset" button will restore the the original text,
the "verbose" option will let you turn on or off verbose results, and
the "configure" checkbox will let you change compilation options.
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.
WebAda will normally use an Ada 95 compiler.
Quiz:
What is the name of the new procedure defined above?
- Text_IO
- Hello
- Put_Line
You may also:
David A. Wheeler (dwheeler@ida.org)
The master copy of this file is at
"http://www.adahome.com/Tutorials/Lovelace/s1s2.htm".