ZetCode

Ada tutorials

Ada is a compiled, statically typed programming language designed for software that must remain correct for a long time. It was created for embedded and real-time systems and is used in air traffic control, satellites, railway signalling, medical devices, and large information systems. Ada puts readability and safety first: every value has a type, ranges and array bounds are checked, packages separate interfaces from implementations, and tasks and protected objects express concurrency in the language itself.

Ada and GNAT

Ada is an ISO/IEC standard, revised from Ada 83 through Ada 95, Ada 2005, Ada 2012, and Ada 2022. The tutorials use GNAT, the Ada front end of the GNU Compiler Collection. On Debian and Ubuntu it is installed with sudo apt install gnat-14 gprbuild; some installations keep the version in the executable names, so the compiler may be called gnatmake-14 instead of gnatmake. The examples are compiled with GNAT 14.3.0.

Getting started

The introduction covers the history and standards of the language, the GNAT toolchain, and the first programs. The basics tutorial starts with with and use, declarations, printing, and comments, then moves on to loops, conditionals, and procedures.

Language

The program structure article explains declarative parts, nested declare blocks, scope and visibility, and named ends. Types introduces Integer, Float, Boolean, Character, and String, and shows why Ada requires an explicit conversion between numeric types. The variables and constants article covers initial values, constant, named numbers, uninitialised variables and compiler warnings, while literals describes decimal and based numbers, real literals, characters, and strings.

The operators and expressions article covers integer division with mod and rem, powers, comparisons, short-circuit Boolean operators, and membership tests. Scalar types and subtypes declares integer types with their own ranges, constrains existing types with subtype, and defines enumeration types, while the attributes article works with 'Image, 'Value, 'Length, 'Range, 'Size, and 'Min and 'Max. The conditionals article uses if, case, and their expression forms, and loops repeats statements with while, for, reverse, named exits, and for ... of. The procedures and functions article introduces parameter modes, defaults, named associations, expression functions, overloading, and recursion.

Packages and data

The packages article separates a specification from a body, hides a type behind a private part, and extends a package with child units. Strings covers the fixed String type, slices, concatenation, and the routines in Ada.Strings.Fixed, while unbounded and bounded strings shows how text can grow and how a maximum length is declared. The arrays article introduces constrained and unconstrained array types, aggregates, slices, and multidimensional arrays. Records groups components of different types and combines them with arrays, and discriminated and variant records lets a discriminant select which components exist.

Storage and errors

The access types article allocates objects with new, dereferences them with .all, handles null, points at existing variables with 'Access, and builds a linked list. Exceptions shows how raise, handlers, and propagation report and recover from errors, including the checks that the runtime performs on ranges, array indices, and discriminants.

Input, output, and the system

Text output aligns integers, reals, and enumeration values with Integer_IO, Float_IO, and Enumeration_IO. The reading input article uses Get_Line, Get, and End_Of_File, and files creates, reads, and appends text files. Sequential and direct I/O stores records in binary files with Sequential_IO, Direct_IO, and Stream_IO. The command-line arguments article reads the program name and the arguments with Ada.Command_Line, parses options, and sets the exit status. Environment and processes reads and changes environment variables with Ada.Environment_Variables and starts a child process with GNAT.OS_Lib.Spawn, and directories creates and searches directories with Ada.Directories.

Generics and object-oriented Ada

Generics introduces formal types and subprograms and builds a generic bounded stack, while containers uses the standard vectors, lists, sets, and maps. The tagged types article adds a run-time tag to a record, declares primitive operations, and extends a type with new components, and inheritance and polymorphism overrides those operations, dispatches through 'Class, and declares abstract types and interfaces. Controlled types runs Initialize, Adjust, and Finalize when objects are created, copied, and destroyed, operator overloading gives a record type its own "+", "=", and "<", and the contracts article adds preconditions, postconditions, invariants, and predicates.

Concurrency

Tasks starts concurrent work and shows activation, termination, delay, and task types, and the rendezvous article synchronizes a caller with a task through entries, select alternatives, guards, and timeouts. Protected objects guard shared data with protected procedures, functions, and entries whose barriers make a caller wait.

Algorithms and data structures

Recursion introduces base cases, the call tree of Fibonacci, memoisation, the Towers of Hanoi, and backtracking, while sorting and searching cover insertion sort, quicksort, merge sort, linear and binary search, and the generic algorithms in the standard library. The linked lists article builds a list package with insertion, deletion, and reversal, and stacks, queues and trees adds a generic dynamic stack, a queue, and a binary search tree. Hash tables hashes integer and string keys, resolves collisions with linear probing, grows the table by rehashing, and compares a hand-written table with Ada.Containers.Hashed_Maps.

Text, numbers, and time

String processing counts words, splits CSV lines, trims headers, classifies characters, and checks palindromes, while number formatting prints integers in other bases, controls floating-point fields, aligns a table, and writes Roman numerals. The fixed-point and decimal types article declares binary and decimal types, keeps money in exact cents, and prints currency pictures, and big numbers computes 100 factorial and exact rationals. Date and time splits, formats, compares, and measures times with the calendar and real-time packages, and random numbers seeds the float and discrete generators, shuffles an array, and counts a histogram.

Tools and practice

Interfacing with C imports functions from the C library, passes strings and doubles, sorts through a callback, and exports an Ada subprogram back to C. Project files and gprbuild organises sources, switches, and static libraries, and unit testing builds a small pass and fail harness, writes table-driven cases, and runs the suite from a project.

Language features

Ada separates the specification of a package from its body, so a program can be compiled against an interface without seeing the implementation. The type system distinguishes integer, floating-point, fixed-point, enumeration, array, record, access, and tagged types, and allows a program to declare new scalar types with their own ranges. Generics provide reusable containers and algorithms, while tasks and protected objects provide concurrency without an external library.

These fifty-two tutorials cover the language from the first program to the C interface, project files, and the test harness. Each article is self-contained, and every example is compiled with gnatmake -gnat2022 -gnatwa (with -gnata where assertions or contracts are involved) and run with GNAT 14.3.0 before its output is shown. The project articles use gprbuild, and the C examples add gcc and -largs.

last modified November 23, 2026