ZetCode

Ada variables and constants

last modified November 23, 2026

A variable is a named object whose value can change during the run. A constant is a named object whose value is fixed when the declaration is elaborated. Ada also has named numbers, which are constants without an explicit type, and it distinguishes variables that have an initial value from variables that begin with no defined value. This article covers all of these forms.

Variable declarations

A variable declaration names a type, a name, and an optional initial value. The initial value is computed when the declarative part is elaborated, before the first statement runs. A later assignment uses := and replaces the value; the single = compares two values and produces a Boolean.

variables.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Variables is
   Count : Integer := 10;
   Step  : Integer := 2;
begin
   Put_Line ("start =" & Integer'Image (Count));
   Step := Step + 1;
   Count := Count + Step;
   Put_Line ("after add =" & Integer'Image (Count));
   Count := Count * 2;
   Put_Line ("after double =" & Integer'Image (Count));
end Variables;

Step becomes 3 before it is added, so Count rises from 10 to 13. The last assignment doubles it to 26. The declarations are read once, but the statements between begin and end can read and write the variables as often as needed.

$ gnatmake -gnat2022 -gnatwa variables.adb
$ ./variables
start = 10
after add = 13
after double = 26

Constants

Adding the keyword constant after the name turns a declaration into a constant. In an ordinary declarative part a constant must have an initial value, because there is no later opportunity to assign one. It is useful for a value that appears in several places: the name documents the meaning, and changing the declaration changes every use.

constants.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Constants is
   Max_Items : constant Integer := 100;
   Greeting  : constant String := "Hello there!";
begin
   Put_Line (Greeting);
   Put_Line ("max items =" & Integer'Image (Max_Items));
end Constants;

Max_Items has type Integer and the value 100. Greeting is a constant string of twelve characters. Both are elaborated before the first call, and neither can be changed later. The compiler may fold a constant into the code that uses it, but the name remains the readable form in the source.

$ gnatmake -gnat2022 -gnatwa constants.adb
$ ./constants
Hello there!
max items = 100

Assigning to a constant

An assignment needs a variable on its left-hand side. When the name denotes a constant, the compiler rejects the statement before the program is linked. The message speaks about the left-hand side rather than about the constant itself, because the same rule applies to any expression that cannot be assigned.

constant_error.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Constant_Error is
   Max_Items : constant Integer := 100;
begin
   Max_Items := 200;
   Put_Line ("max items =" & Integer'Image (Max_Items));
end Constant_Error;

The declaration is correct; only the assignment is wrong. Removing constant makes Max_Items a variable and the program compiles, but then the name no longer promises that the value stays fixed. Choosing between the two forms is a way of stating intent in the source.

$ gnatmake -gnat2022 -gnatwa constant_error.adb
x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa constant_error.adb
constant_error.adb:7:04: error: left hand side of assignment must be a variable
gnatmake: "constant_error.adb" compilation error

Named numbers

A named number is a constant declared with := but without a type name. Its value belongs to the universal numeric types, and it takes the type that the context requires. Named numbers are convenient for mathematical constants, counts, and sizes that are used with more than one numeric type.

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

procedure Named is
   Pi            : constant := 3.14159;
   Radius        : constant Float := 2.0;
   Circumference : constant Float := 2.0 * Pi * Radius;
begin
   Put_Line ("circumference =" & Float'Image (Circumference));
end Named;

Pi has no explicit type, so it can multiply the Float values without a conversion. Circumference is a Float and is printed in the default image format. A named integer such as Max_Items : constant := 100; can be used as a loop bound, an array size, or an integer value, and the compiler chooses the type from the use.

$ gnatmake -gnat2022 -gnatwa named.adb
$ ./named
circumference = 1.25664E+01

Uninitialised variables

A variable declared without := has no defined initial value. For a scalar type such as Integer, reading it before assigning it is a bounded error: the program may see an arbitrary value, and the compiler is not required to diagnose it. GNAT warns about the common case when warnings are enabled.

uninitialised.adb
with Ada.Text_IO;
use Ada.Text_IO;

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

The right-hand side reads Count before the assignment stores anything in it. GNAT reports the problem even without -gnatwa, since the warning is enabled by default, but the compiler still produces an executable. Giving the declaration an initial value, or assigning the variable before the first read, removes both the warning and the undefined behaviour.

$ gnatmake -gnat2022 -gnatwa uninitialised.adb
x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa uninitialised.adb
uninitialised.adb:7:13: warning: "Count" may be referenced before it has a value [enabled by default]
x86_64-linux-gnu-gnatbind-14 -x uninitialised.ali
x86_64-linux-gnu-gnatlink-14 uninitialised.ali

Swapping values

Assignment copies a value, so swapping two variables needs a third variable to hold one value while the other is moved. The temporary can be declared without an initial value as long as it is assigned before it is read. This is the same sequence that a swap needs in most languages without multiple assignment.

swap.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Swap is
   Left  : Integer := 1;
   Right : Integer := 2;
   Temp  : Integer;
begin
   Temp := Left;
   Left := Right;
   Right := Temp;
   Put_Line ("left =" & Integer'Image (Left));
   Put_Line ("right =" & Integer'Image (Right));
end Swap;

After the three assignments, Left holds 2 and Right holds 1. The order matters: assigning Left := Right first would overwrite the original value of Left, and the temporary is what preserves it. Ada has no swap statement for two scalar variables, so the temporary assignment is written explicitly.

$ gnatmake -gnat2022 -gnatwa swap.adb
$ ./swap
left = 2
right = 1

Next steps

The literals article shows every form that the initial values can take, including underscores, based numbers, and quoted text. The next article in the series, operators and expressions, continues with the expressions that combine variables and constants.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have declared variables with and without initial values, fixed values with constant, seen the compiler reject an assignment to a constant, used a named number, examined an uninitialised variable with GNAT warnings, and swapped two values. The next article describes the literals that these declarations use.

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.