Ada/Introduction

From Wikiversity
< Ada
Jump to navigation Jump to search

Overview[edit | edit source]

We will be writing the famous Hello World program in Ada in this lesson. We will talk about the history of the language, the language structure, and what each line of our program does.

History of Ada[edit | edit source]

Origins[edit | edit source]

Development[edit | edit source]

Early Days[edit | edit source]

Modern Usage[edit | edit source]

Language Structure[edit | edit source]

Hello, World![edit | edit source]

Here we go! This is the program you've been waiting to see. If you want to write this yourself here are the steps on Linux:

  1. Open the terminal
  2. Type
    emacs Hello.adb
  3. Type the following code
with Ada.Text_IO;

procedure Hello is
begin
   Ada.Text_IO.Put_Line("Hello!");
end Hello;

Before we compile this we need to save the file. To do this using the keyboard shortcuts in Emacs, hit Ctrl-x Ctrl-s. Now to compile this program. You may have noticed at the top of the Emacs toolbar that there is an option called "Ada". Under that option you will see the option to compile the program and also run it. First compile the program to make sure no errors are present in the code. Then you can run the program and see the output in that bottom Emacs buffer that appears when you compile.

Analyzing Hello, World![edit | edit source]

Now what exactly does each line in that program do?

with Ada.Text_IO;

This line tells the compiler that we need to be able to use the Text_IO package contained in the larger Ada package. You always need to tell compiler what packages you will be using with your Ada program using similar with statements. Note the semicolon at the end of the statement. Most languages based on Algol, such as C++ and Ada, use a semicolon to indicate that a particular statement has ended and that the next one should be executed. Leaving out the semicolon will generate an error at compile time in most cases.

procedure Hello is

This line is how functions in Ada, called procedures (hence the procedure keyword), are defined. This is only the very first part of defining a procedure. We then need these next critical lines:

begin...end Hello;

The code contained between these begin and end keywords determine how the procedure will work. The code length for procedures, and functions in general, can be very short, very long, or anywhere in between. It just depends on what needs to happen when that procedure is called. Again, note the semicolon. In other languages we use the curly braces {} to indicate that we are defining a function. In the case of Ada, the begin and end keywords mean essentially the same thing and the semicolon right after end means that that particular procedure is finished.

In the procedure definition we have this line:

Ada.Text_IO.Put_Line("Hello!");

Wait a minute? Did we not tell the compiler that we were using Ada.Text_IO? Why do we have to say it again? Well, Ada has a pretty cool feature that allows you to rename packages temporarily and we will be talking a bit more about it later. But for now, since Ada.Text_IO was not renamed, we need to have it as shown. That next part Put_Line is a function defined under Text_IO. This allows us to output text to the console. Notice how we have parentheses with quotation marks to tell that function what to output. This is true of most Algol-based languages as well. A string, or text in general being output to the console, needs to be enclosed in quotation marks so the compiler knows how to handle it appropriately. This statement also ends with a semicolon, just like every other statement we have looked at so far.

That's pretty much a basic analysis of this program. This follows a pretty basic structure of almost every program we will be writing and looking at:

  1. Bring in needed packages
  2. Define procedures, the main procedure being the first one defined with subsequent ones following