ZetCode

Ada program structure

last modified November 23, 2026

Every Ada subprogram has two parts: a declarative part, where names are introduced, and a sequence of statements that runs after the declarations. A declare block adds another declarative part inside the statements, and the rules of scope decide which name a reference reaches. This article describes those pieces and the optional names written after end.

The declarative part

A procedure begins with its name and parameters, followed by is. The declarations between is and begin are elaborated in order before the first statement runs. Each declaration may include an initial value, which is computed once at that moment. The statements then run in order until end Structure;.

structure.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Structure is
   Count : Integer := 2;
begin
   Count := Count + 1;
   Put_Line ("count =" & Integer'Image (Count));
end Structure;

Count starts with 2, the statement raises it to 3, and the call prints the result. The name after end repeats the procedure name; it is optional, but the compiler checks it when it is present. The word is belongs to the procedure, not to the declaration, so a procedure with no declarations still has it.

$ gnatmake -gnat2022 -gnatwa structure.adb
$ ./structure
count = 3

Declare blocks

A declare block introduces a new declarative part in the middle of a sequence of statements. It starts with declare, contains declarations, then begin, then its own statements, and ends with end;. Names declared inside the block exist only until that end, which makes the block useful for a temporary value that should not be visible to the rest of the procedure.

block.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Block is
   Outer : Integer := 10;
begin
   Outer := Outer + 1;
   declare
      Inner : Integer := 5;
   begin
      Inner := Inner * 2;
      Put_Line ("inner sum =" & Integer'Image (Outer + Inner));
   end;
   Put_Line ("outer =" & Integer'Image (Outer));
end Block;

The outer variable becomes 11, and the inner one becomes 10, so the block prints 21. After end; the inner name is gone, but Outer is still visible and still holds 11. The block does not repeat a name after end, because it has no name of its own unless one is put before declare.

$ gnatmake -gnat2022 -gnatwa block.adb
$ ./block
inner sum = 21
outer = 11

Nesting and scope

An inner declaration may use the same name as an outer one. The inner declaration hides the outer name for the rest of its block, and the outer name becomes visible again when the block ends. The two variables are separate objects: assigning to the inner one does not change the outer one.

scope.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Scope is
   Value : Integer := 1;
begin
   Value := Value + 1;
   Put_Line ("outer value =" & Integer'Image (Value));
   declare
      Value : Integer := 2;
   begin
      Value := Value * 10;
      Put_Line ("inner value =" & Integer'Image (Value));
   end;
   Put_Line ("outer value =" & Integer'Image (Value));
end Scope;

The outer Value becomes 2 and is printed before the block. Inside the block, a new Value starts at 2 and becomes 20. When the block ends, the inner object disappears and the final call sees the outer variable, which is still 2. This is lexical scoping: the text of the program decides which declaration a name refers to.

$ gnatmake -gnat2022 -gnatwa scope.adb
$ ./scope
outer value = 2
inner value = 20
outer value = 2

Visibility

A declaration is visible only after the point where it appears. The declarative part is not a list that can be read in any order: an initial value that mentions a later name is rejected. The compiler reports the unknown name and stops before linking.

visibility.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Visibility is
   First  : Integer := Second;
   Second : Integer := 5;
begin
   Put_Line ("first =" & Integer'Image (First));
end Visibility;

Second is declared on the next line, so it is not yet visible in the initial value of First. Swapping the two declarations fixes the program. The same rule applies inside a declare block and in a package specification.

$ gnatmake -gnat2022 -gnatwa visibility.adb
x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa visibility.adb
visibility.adb:5:24: error: "Second" is undefined
gnatmake: "visibility.adb" compilation error

Named ends

The name after end may be repeated for procedures, functions, packages, blocks, and loops; an if statement cannot be named, and end if Check; is a syntax error. For a loop the name is placed before the loop as a label, as in Outer_Loop :, and the same name is written after end loop. Named ends make deep nesting easier to read and let the compiler catch a mismatched closing name.

named.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Named is
   Total : Integer := 0;
begin
   Outer_Loop :
   for I in 1 .. 3 loop
      Inner_Loop :
      for J in 1 .. 2 loop
         Total := Total + I * J;
      end loop Inner_Loop;
   end loop Outer_Loop;
   Put_Line ("total =" & Integer'Image (Total));
end Named;

The outer loop runs three times and the inner loop twice, so six products are added. The inner pass contributes 1 and 2 for I = 1, 2 and 4 for I = 2, and 3 and 6 for I = 3, which totals 18. end loop Inner_Loop; and end loop Outer_Loop; state which loop each one closes.

$ gnatmake -gnat2022 -gnatwa named.adb
$ ./named
total = 18

Next steps

The types article describes the values that these declarations hold, and the variables and constants article explains initial values and named numbers. The basics article shows the same structure in complete programs.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have separated declarations from statements, opened a nested declare block, followed a name through two scopes, seen why a later declaration is not visible earlier, and named the ends of nested loops. The next article turns to the types of the declared values.

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.