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.
There is only one type of comment in Ada — those prefixed by “–”.
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.
Arg1 := 12; -- changes the value passed to 12
function Set_Var return Integer is
Var : Integer := 10; -- sets var to 10
Modify_Args(Var); -- passes it to the procedure which changes it to 12
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.
function foo return Float; -- must be implemented in package body or error will result
function foo return Float is
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.
procedure Test is
Well, that’s it for now. I’ll be back to update with some more interesting features of Ada.