Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

Variables, Constants and Declarations in Ada

Declare variables, constants and named numbers in Ada, learn initialization, the := assignment, declare blocks, scope and why Ada makes you name a type for everything.

In Ada every object has a type that you choose, and the compiler never guesses one for you from a literal. That sounds strict, and it is, but it removes a whole family of bugs. This lesson covers the three kinds of declarations you meet first: variables, constants and named numbers, plus scope and declare blocks.

Ada listings are read-only here; runnable C stand-ins follow each idea.

Variables

A variable declaration has the form Name : Type [:= Initial_Value];:

ADA
procedure Demo is
   Count   : Integer := 0;
   Ratio   : Float   := 0.5;
   Letter  : Character := 'A';
   Done    : Boolean := False;
   Name    : String (1 .. 5) := "Ada  ";
begin
   Count := Count + 1;
end Demo;

You can declare several names at once (A, B, C : Integer := 0;). Assignment uses :=; equality uses =; inequality uses /=. A variable without an initial value holds an arbitrary value (it is uninitialized), and using it is a bug that GNAT often warns about. Initialize on declaration.

Constants

Add the word constant and the object can never change:

ADA
Max_Users : constant Integer := 100;
Pi        : constant Float := 3.141_59;
Banner    : constant String := "Welcome";   --  bounds come from the value

For a constant string, you can omit the bounds because the initial value supplies them. A constant need not be computed at compile time: Start : constant Integer := Read_Config; is legal, and is initialized when it is elaborated.

Named numbers

A named number is a compile-time constant of no particular type:

ADA
Seconds_Per_Day : constant := 24 * 60 * 60;
Two_Pi          : constant := 2.0 * 3.141_592_653_589_793;

There is no type before the :=. A named number is exact: the compiler uses unlimited-precision arithmetic, so Seconds_Per_Day can be used as an Integer, a Long_Integer or a float without a conversion, as long as the value fits. This is one reason Ada code rarely needs a preprocessor.

Declarations come first

Declarations live in the declarative part, before begin. When you need a temporary variable in the middle of the code, open a declare block:

ADA
begin
   Put_Line ("Start");
   declare
      Temp : Integer := Compute;
   begin
      Put_Line (Integer'Image (Temp));
   end;   --  Temp ceases to exist here
   Put_Line ("Done");
end Demo;

A declare block creates a nested scope. Names declared in it are not visible outside. This is how Ada does what other languages do with { int temp = ...; }.

Scope and visibility

A name is visible from its declaration to the end of the enclosing block. An inner declaration with the same name hides the outer one, and Ada compilers can warn about it. You can still reach the outer name by prefixing it with the enclosing unit's name (Demo.Count).

flowchart TD
    A["procedure Demo<br/>Count : Integer"] --> B["declare block<br/>Temp : Integer"]
    B --> C["sees Count and Temp"]
    A --> D["after the block:<br/>sees Count only"]

Inner scopes can see outer names; outer code cannot see inner names.

No implicit types

Ada has nothing like auto or var. If you want a value of type Integer, you say Integer. Literals are universal: 42 can become any integer type that can hold it, and 3.14 any real type, but an expression like Count + 0.5 is an error because Count is an integer and 0.5 is real. You must convert explicitly: Float (Count) + 0.5.

ADA
Count : Integer := 7;
X     : Float   := Float (Count) * 1.5;   --  explicit conversion

Stand-in: constants and scopes in C

The C program below mirrors Demo: a constant, a variable, a nested scope that hides an outer name, and an explicit conversion. In C the compiler would do the int-to-double conversion silently; the cast makes it visible, as Ada demands.

C Stand-in: constants, scopes and conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice that the inner count does not disturb the outer one. In Ada the same shadowing triggers a warning because it is easy to misread.

Uninitialized variables are another classic hazard. This stand-in shows what Ada's pragma Normalize_Scalars and GNAT's -gnatVa try to catch: a variable with a sentinel "invalid" value that is detected before use.

C Stand-in: catching uninitialized values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Common mistakes

  • Using = instead of := for assignment.
  • Forgetting that constant needs an initial value.
  • Mixing Integer and Float in one expression without a conversion.
  • Declaring a variable after begin without a declare block.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a named number such as Limit : constant := 1000;?

    A named number has no type in its declaration; it is a universal compile-time value that converts to any suitable numeric type.
  2. 2. How do you create a temporary variable in the middle of the statements?

    A declare block provides a new declarative part with its own scope.
  3. 3. Which expression is legal when N : Integer and X : Float?

    Ada never converts between numeric types implicitly; you write the conversion.
  4. 4. What does A /= B mean?

    /= is Ada's inequality operator.

Summary

  • Variables: Name : Type := Value;. Constants add constant. Named numbers omit the type.
  • Assignment is :=; comparison uses = and /=.
  • Declarations belong before begin; use declare blocks for inner scopes.
  • No implicit typing and no implicit numeric conversion.

Next: Text_IO: Reading and Writing Text