Tutorial

Declarations

Declaration lines must contain a name for the type being declared.  A type must be entered in the declaration line unless a number declaration is being performed.  Variables can be declared as integers, floats, strings, characters, or booleans.

K : Integer := 3;
L : Float := 3.334;
M : String (1..20) := "What is your name?  ";
N : Character := '&';
P : Boolean;

When a constant is declared, the key word constant is added to the declaration line.

X : constant Integer := 500;

In this case X is declared to be an integer with the value of 500.  Once X has been declared as a constant it can not be changed.  Floats and characters can also be declared as constants.

If you want to do a number declaration, you do not have to declare the type.

Pi : constant := 3.14159;

Number declarations can only be made for numeric values.

Number declarations are very simple to do in Ada.  Standard integer declarations are used in the program to print out Fibonacci numbers shown in Figure 2.  This program was written and tested on a GNAT compiler.

Fibonacci sequence is defined by the equations in Figure 1.
 
 
 

f0 = 1,
f1 = 1,
fn+1 = fn + fn-1
Figure 1   Fibonacci Sequence

 
 
with Ada.Integer_Text_IO, Ada.Text_IO;
use Ada.Integer_Text_IO, Ada.Text_IO;
procedure Fibonacci_Numbers is
   Next_Number : Integer; -- integer declaration with Next_Number being defaulted to the null value
   F0, F1: Integer := 1; -- integer declaration with f0 and f1 being set to 1
   Number_Printed : Integer range 1 .. 46;  -- integer declaration with a range from 1 to 46
begin
   Put (1);
   Number_Printed := 2;
   while Number_Printed <= 45 loop
      Next_Number := F0 + F1;
      Put (Next_Number);
      New_Line;  -- Uses Ada.Text_IO
      Number_Printed := Number_Printed + 1;
      F1 := F0;
      F0 := Next_Number;
   end loop;
end Fibonacci_Numbers;
Figure 2   Ada 95 program for Fibonacci Sequence

 
 

Go to the Tutorial Table of Contents