ZetCode

Introduction to Ada

last modified November 23, 2026

Ada is a compiled, statically typed programming language designed for software that has to remain correct for a long time. It was created for embedded and real-time systems, where a wrong value or an unchecked array index can have serious consequences, and it has since been used for air traffic control, satellites, railway signalling, medical devices, and large information systems. This article introduces the language, lists its standards, describes the GNAT toolchain, and shows the first programs.

What is Ada

Ada was commissioned by the United States Department of Defense in the 1970s. The department used hundreds of programming languages in its projects and wanted one language with a precise definition, portable compilers, and support for large programs. The language was named after Ada Lovelace, who wrote the first published algorithm intended for a machine in the nineteenth century. The first standard appeared in 1983.

The design puts readability and safety before brevity. Every value has a type, and the compiler rejects mixtures that are not explicitly converted. Array bounds, subtypes, and numeric ranges are checked at run time by default. The programmer can state assumptions as contracts, and the compiler can verify some of them before the program runs. These checks cost time, but they turn many silent errors into immediate exceptions.

The language is built from a small number of ideas. A package groups declarations behind a specification, so a program depends on an interface rather than on an implementation. Generics let a package or subprogram be written once and instantiated for several types. Tasks and protected objects express concurrency in the language itself, and tagged types add inheritance and dynamic dispatch. Ada 2012 added contracts, and Ada 2022 added parallel loops, big integers, and image attributes for every type.

The Ada standards

Ada is an international standard, maintained by ISO/IEC and revised about once a decade. Each revision keeps the previous one compatible in almost all cases, so a program written for Ada 83 usually still compiles today. The revisions added features rather than replacing the core design.

StandardNotable additions
Ada 83packages, generics, exceptions, tasking, separate compilation
Ada 95tagged types and inheritance, protected objects, child packages, access-to-subprogram types
Ada 2005interfaces, the container library, more flexible access types, the Ravenscar profile
Ada 2012contracts with Pre and Post, expression functions, aspect specifications, more containers
Ada 2022parallel loops, 'Image for every type, big integers, additional container operations

This series uses Ada 2022 as its baseline. Most examples also compile as Ada 2012, and the few that need a newer feature are marked in the text. The compiler is told which revision to accept with the -gnat2022 switch.

GNAT and the GNU toolchain

The examples in this series are compiled with GNAT, the Ada front end of the GNU Compiler Collection. GNAT is free software, runs on Linux, Windows, and macOS, and is the compiler used by AdaCore and by most Ada projects. On Debian and Ubuntu, the compiler and the project builder are installed from the distribution:

sudo apt install gnat-14 gprbuild

The package keeps the version in the executable names on some installations, so the compiler may be called gnatmake-14 rather than gnatmake. The commands in this article use the unversioned names; substitute the versioned name when your system has only that one. gnatmake compiles a source file, binds the result, and links an executable. It finds the other compilation units that the program needs and rebuilds only what has changed. A project with many units is usually described by a .gpr file and built with gprbuild, which is covered later in the series. The installation used here reports:

$ gnat --version
GNAT 14.3.0
Copyright (C) 1996-2024, Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The first program

An Ada program is a collection of compilation units. The simplest complete program is a procedure with no parameters, and every procedure has a declarative part between is and begin. The declarations are elaborated first, then the statements run in order, and end Hello; closes the unit and repeats its name.

hello.adb
with Ada.Text_IO;

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

The with clause makes the package Ada.Text_IO available to the program; it is the Ada form of an import. The procedure Put_Line writes its string argument and then ends the line. The call is written with the full package name because no use clause is present, which keeps the origin of the name visible.

Ada is case-insensitive for identifiers and keywords, so Put_Line, put_line, and PUT_LINE are the same name. The convention is to capitalize each word of an identifier and to write keywords in lower case, as in the example. A semicolon ends every declaration and statement, and a string is enclosed in double quotes.

$ gnatmake -gnat2022 -gnatwa hello.adb
$ ./hello
Hello there!

Compiling and running

gnatmake accepts the usual GNAT switches. -gnat2022 selects the Ada 2022 language revision, and -gnatwa turns on all optional warnings, including warnings about variables that are never assigned or could be constants. The compiler writes the object file and the library information file beside the source; the second file has the .ali extension and records what the compiler needs to know about the unit.

The executable is named after the main procedure, so the program above becomes hello. The build products can be removed with gnatclean, which reads the .ali file and deletes the files that belong to the unit:

$ gnatclean -c hello
"./hello.ali" has been deleted
"./hello.o" has been deleted

A second program

A program may contain any number of statements. The next example prints three lines and repeats the package name on each call. Nothing is declared in the declarative part, which is allowed: the part may be empty, and the statements begin directly after begin.

facts.adb
with Ada.Text_IO;

procedure Facts is
begin
   Ada.Text_IO.Put_Line ("Ada is a statically typed language.");
   Ada.Text_IO.Put_Line ("The first standard was published in 1983.");
   Ada.Text_IO.Put_Line ("GNAT is part of the GNU Compiler Collection.");
end Facts;

Each call writes one line, and the calls run in the order in which they appear. There is no implicit output and no formatting string: the argument is the text to write. Text I/O is covered in detail later, including how to print numbers and how to control the width and alignment of the output.

$ gnatmake -gnat2022 -gnatwa facts.adb
$ ./facts
Ada is a statically typed language.
The first standard was published in 1983.
GNAT is part of the GNU Compiler Collection.

File names and library units

Every compilation unit is stored in a file whose name matches the unit name. The procedure Hello lives in hello.adb, and a procedure Main lives in main.adb. The file name is written in lower case, while the unit name may use mixed case. A package has two files: the specification shapes.ads declares the interface and the body shapes.adb contains the implementation. Packages are the subject of a later article.

GNAT checks the file name when it compiles a unit. A procedure named Other stored in wrong.adb still builds, but the compiler reports the mismatch:

wrong.adb:3:11: warning: file name does not match unit name, should be "other.adb" [enabled by default]

The warning is easy to miss in a long build log, and some tools rely on the file name to find a unit. Keeping the names equal avoids the problem altogether. The same rule applies to .ads and .adb files, and to the file name of a generic unit.

Next steps

The Ada basics article continues with declarations, printing, comments, loops, conditionals, and the first procedures. The program structure article explains the declarative part and nested blocks, and the types article describes the predefined types and the conversions between them.

Source

Ada standards and the Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

Ada on Wikipedia

In this article we have introduced Ada, surveyed its standards from Ada 83 to Ada 2022, installed GNAT, and compiled and run the first two programs. The next article turns these examples into a working knowledge of declarations, control flow, and procedures.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.