Structure

General Structure

Ada processes are composed of three parts.

Declarative Header

The declarations go here.

Necessary to store the memory space.

All variables must be specified with type. You can also initiate them here.

Goes between is .. begin.

Declaration syntax

<name> [, <name2> ...] : <type> [ := <value>];

Declaration examples

My_Number: Integer;
My_Other_Number, Zero : Constant Integer := 0

The main types are the same as in C, but no acronyms are used. C programmers tend to say Ada is verbose, but it's only natural from the daughter of a Poet.

Ada was designed with the humans in mind, so verbosity is a feature, not a bug. One that you will find extremely useful when you are part of multidisciplinary teams.

The language uses two main sources for it's meanings: English Language and Mathematical Notation.

Algorithmic Body

The algorithm and calls to other processes go here. The actual code.

It's what you probably understand as program.

Goes between begin .. end or between begin .. exception.

Assignation syntax

<name> := <value>;

Assignation examples

My_Number := 3;
My_Other_Number, And_Yet_More_Numbers : Constant Integer := 0;
boolean1 := True = False; --Comparison operator = AS IN SCHOOL MATHS!!!

When we are both declaring and assigning we call it Defining a variable.

The colon symbol ":" is read as mathematical notation "defined as", but many times we use the shortcut "is", so ":=" should be interpreted as "is defined as equal to", "is equal" or "equal by definition to". Or just "is" when you feel lazy and the context is clear.

The symbol "=" is the comparative equal you know (and love) from Mathematics. Not much more to say here, just that it was C who confused the assignation and the comparison, something highly criticized at the moment and still today. "==" was introduced to mass confusion of mathematicians and engineers, and the isolation of the IT field furthered with java's catastrophic "===".

And yes, is not that hard to change one symbol's meaning on your head, but you go against 600 years of a really hard process of mathematical history, and everything you learnt in school, high school, and all non-IT classes you studied in your life. So why not redefining the symbols "+" to mean multiplication and "++" to mean sum, oh and to increment "+++", but just in java.

Exception Footer

This part is optional, but recommended.

It establishes the course of action in case some exception happens.

Exception syntax

--Exception Declaration

<name> : Exception;
Ups : Exception

--Exception Raise

raise Ups;

--Exception Control

exception
 when Ups => Ring_Alarm();

Processes

Processes are the basic units of work in a program. Where C only knows Functions, Ada differentiates Procedures (void functions) and functions.

Procedures take actions.

Functions produce data.

Functions

Functions are processes that produce data.

They must be called at the right side of an assignation statement.

Procedures

Procedures are processes that do something.

They must be called as an statement by themselves.

Procedures

procedure <name> [(<arguments)] is
 [<variable declarations>]
begin
 <code>
[<exceptions>]
end name;
foo(par);

Functions

function <name> [(<arguments)] 
 return <type> is
 [<variable declarations>]
begin
 <code>
 return <value_of_type>;
[<exceptions>]
end name;
var = foo(par);

The code is not optional, even if you want to have a null statement. You must then be explicit and write a "null" code-word.

Arguments

The arguments are optional, but most of the time necessary. They are the data that a program module needs to properly do the task at hand.

There are three types of arguments of a process:

in

<name>: in <type>;

out

<name>: out <type>;

in out

<name>: in out <type>;

Input Arguments are the default value. The can be read but not modified by the process. Equivalent to a call by constant value.

Output Arguments can be modified but not read by the program. Equivalent to a call by ... (is there an equivalence for this? If so, email me. I'm curious.)

In-Out arguments can be read and modified by the process. Equivalent to a call by reference.

Arguments must be of the proper type (understand class here) or you will get a run time error.

Examples

in

Ping( To: in IP);

out

Set_Time(time: out clock);

in out

Swap(A,B: in out <>);

Also, Arguments can be defined with a default value in case of a missing parameter value.

<name> : [<in/out>] <type> [:= <value>]

Examples

procedure Sum(A,B: in Integer; C: out Integer) is 
begin
 C:=A+B;
end Sum;
procedure Swap(A,B: in out <>) is
 temp: <> := A;
begin
 A:=B;
 B:=Temp;
end Swap;
function Max
 (A,B : in integer)  
 return integer 
is
begin
 if A>B then return A;
 else return B;
 end if;
end Max;
PROCEDURE Increment(a: IN OUT INTEGER, b: IN INTEGER := 1) IS
BEGIN
  a:= a+b;
END Increment;

Calls

To call a process we just write the name of the module with the parameters (values or variables) in order, or we can call by keyword.

The variables as parameters can share, or not, the name of the arguments.

Functions must be at the right side of a assignation, or as a condition. Processes must stand on their own as a statement.

Parentheses can be omitted if no parameters are provided.

a:= Max(a,100);
a:= Max(a => a, b => 100);
Increment(a => my_wallet_content);