This is part of a series on programming in the Ada language, a mission critical programming language that I am attempting to learn and become proficient in.

Well, it’s been a few days and it’s now time to report back with some findings. Note that this isn’t the first time I’ve seen Ada code so I may appear to be going a bit faster than someone completely new to the language would.

Comments

There is only one type of comment in Ada — those prefixed by “–”.

-- This is a comment

Semicolons

Yes, there are semi-colons after just about every complete statement. That’s quite standard among all the major compiled languages. The fact that it shows up in a language known for helping programmers avoid mistakes and improving readability — which semi-colons do help with, as annoying as they are to type.

Assignment vs Equality Check

Many newcomers to programming get confused by the statement “i = i + 1″ which is a common way to increment a variable in some languages. That’s because we have long learned that “=” means the two sides are the same when obviously i cannot equal i + 1. That’s because the “=” in that statement means assignment and changes the value of i to i + 1 rather than specifies that they are equal. That leads to the next big mistake many newcomers make: using the “=” instead of “==”. Nowhere in math is “==” ever used despite its appearance as a mathematical operator. It means, in many languages, the equivalent of what “=” means in math and the rest of the world.

Ada resolves this issue with a simple change: by making the assignment operator “:=” rather than just “=” which it uses in place of “==”. Yes, it’s one extra character to type, but at least it now makes sense to the rest of the world.

I should also point out that the errors generated when you forget that assignment is “:=” and equality is “=” are very helpful and concise in located what you did wrong. Unlike the error system in most C compilers which would say something like “Expected ‘:=’ near ‘=’”, or, even worse, “Unexpected character near ‘=’”, the Ada compiler actually says “‘=’ should be ‘:=’”.

Functions & Procedures

Ada is one of those languages that distinguish between functions and procedures. A function returns a value and cannot modify any arguments passed to it. A procedure does not return a value but can modify arguments.

procedure Modify_Args (Arg1 : in out Integer) is
begin
   Arg1 := 12; -- changes the value passed to 12
end Modify_Args;

function Set_Var return Integer is
   Var : Integer := 10; -- sets var to 10
begin
   Modify_Args(Var); -- passes it to the procedure which changes it to 12
   return Var;
end Set_Var;

Packages

All Ada programs are written as packages. Packages are like classes in object-oriented languages where everything inside is static — so no initializing objects from them.

Packages are typically broken into a spec file and a body file. These resemble the header and code files in C/C++ with one distinct difference — they absolutely must match up. A package body cannot have any public functions or procedures that do not appear in the package spec. This makes sense — a public function/procedure is expected to be used by functions/procedures in other packages which requires inclusion in the spec file.

Package spec:

package Example is
   function foo return Float; -- must be implemented in package body or error will result
end Example;

Package body

package body Example is
   function foo return Float is
   begin
      return 10.0;
   end foo;
end Example;

Even the entry point (“main” program) is included in a package. The entry point has the same name as the package it is included in. Naturally, not every package NEEDS an entry point just as there aren’t multiple “main” functions in C.

The package containing main is specified in the build file. The following program does…well, nothing.

package Test is
   procedure Test is
   begin
      null;
   end Test;
end Test;

Well, that’s it for now. I’ll be back to update with some more interesting features of Ada.

Tagged with →  
Share →

2 Responses to Basics of Ada

  1. François says:

    Nice blog article. Just one comment on the last part.
    There is no need to include the main in a package. The main procedure is just a parameterless procedure.
    Cheers

    • North says:

      I added a clarification to say that a “main” isn’t needed in every package and described it as an “entry point” to avoid confusion on the procedure’s actual name.

      Thanks for the tip.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>