Home

Variables

 

Variables Fundamentals

 

Introduction

To represent the values of an application, a computer language uses a specific category for each type of value. The category is referred to as data type.

  

Declaring a Variable

Before using a variable, you must declare it. You have various options.

To declare a variable, in the line under procedure, use the following formula:

VariableName : DataType;

The declaration starts with the name of the variable:

  • The name of a variable must be in one word
  • It must start with a letter
  • It can include a combination of letters, digits, and underscores
  • It must not contain special characters

The above formula is used to declare one variable. You can use it to declare various variables, each on its own line. This would be:

VariableName1, VariableName2 : DataType1;

If you are declaring more than one variable but those variables use the same data type, you can declare them on the same line, separating their names wit commas. This would be done as follows:

VariableName1 : DataType1;
VariableName2 : DataType1;

Ada uses some words that you must not use to name your variables. These are referred to as reserved words. They are:

abort abs abstract accept access
aliased all and array at
begin body case constant declare
delay delta digits do else
elsif end entry exception exit
for function generic goto if
in interface is limited loop
mod new null not of
or others out overriding package
pragma private procedure protected raise
range record rem renames requeue
return reverse select eparate subtype
synchronized tagged task terminate then
type until use when while
with xor      
 

Initializing a Variable

Initializing a variable consists of assigning a value to it before using it. You have two options.

To initialize a variable when declaring it, after the name of the variable, type := followed by an appropriate value. This would be done as follows:

VariableName : DataType := Value;

The option consists of assigning a value after declaring it. This can be done as follows:

VariableName : DataType
begin
    VariableName:= Value;
end
 

The declare Keyword

We saw that, to declare a variable, you can use the section under the procedure. As another option, in the body of the procedure, use the declare keyword, then declare the variable(s). Before using the variable(s), start a section with begin, include the necessary code, and end it with end;. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   
begin
    declare
        -- Declarations
    begin
        -- Initializations
    end;
end Exercise;

Displaying the Value of a Variable

One way you can use a variable consists of displaying its value to the user. To do this, you can use Put_Line(). If the variable represents a word or sentence (a string), you can write the name of the variable in the parentheses. Otherwise, we will see other ways of displaying it.

Data Types

 

Characters

A character is a a letter, a symbol, or a digit. To declare a variable that can hold a variable, use the character keyword. To initialize it, include the value is single-quotes. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   gender : character := 'M';
begin
   Put_Line("Gender = " & character'image(gender));
end Welcome;

This would produce:

Gender = 'M'

 

Strings

A string is a combination of characters. To represent strings, Ada uses the String data type. When declaring the variable, to initialize it, include its value in double-quotes.

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   sentence : String := "Welcome to the wonderful world of Ada programming!";

begin
    
end Exercise;

To declare a string variable, in the parentheses of Put_Line(), include the name of the variable. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   sentence : String := "Welcome to the wonderful world of Ada programming!";

begin
    Put_Line(sentence);
end Exercise;

This would produce:

Welcome to the wonderful world of Ada programming!

In the same way, you can declare as many variables as you want.

 
 
 

Concatenating Strings

One of the most regular operations you can perform with strings consists of adding one to another. This is referred to as concatenating.

To concatenate two strings, you use the & operator. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
begin
    Put_Line("Gertrude" & "Monay");
end Exercise;

This would produce:

GertrudeMonay

In the same way, you can concatenate as many strings as you want. Here are examples:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
begin  
    Put_Line("Full Name: " & " Gertrude" & " " & "Monay");
end Exercise;

In the same way, you concatenate string variables. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   FirstName : String := "Gertrude ";
   LastName  : String := "Monay";

begin  
    Put_Line(FirstName & LastName);
end Exercise;

You can also concatenate constant strings and string variables. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   FirstName : String := "Gertrude";
   LastName  : String := "Monay";

begin  
    Put_Line("Full Name: " & FirstName & " " & LastName);
end Exercise;

Integers

An integer is a numeric value for a natural number. In Ada, an integral value is represented with the integer data type.

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   number : integer;

begin
    
end Exercise;

Initialize an integer variable with a numeric value without special characters. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   number : integer := 214685;

begin
    
end Exercise;

To display the value of the variable, in the parentheses of Put_Line(), type natural'image() and, in the parentheses, type the name of the variable. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   number : integer := 214685;

begin
    Put_Line("Number: " & natural'image(number));
end Exercise;

This would produce:

Number:  214685

A natural number can include an underscore but would have the same value as if the underscore did not exist. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   number : integer := 2146_85;

begin
    Put_Line("Number: " & natural'image(number));
end Exercise;

You can also write a natural number in scientific format. To do this, use either e or E. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   value1 : integer := 1558_2;
   value2 : integer := 2241;
   value3 : integer := 605E5;
begin
   Put_Line("Value 1 = " & integer'image(value1));
   Put_Line("Value 2 = " & integer'image(value2));
   Put_Line("Value 3 = " & integer'image(value3));
end Welcome;

This would produce:

Value 1 =  15582
Value 2 =  2241
Value 3 =  60500000

Floating Point Numers

 

Introduction

A floating-point number is one that has a decimal separator. In US English, the decimal separator is the period. Examples of floating-point numbers are 12.58 or 27597.0249.

To declare a floating-point variable, use the float keyword. To initialize it, assign a number that includes one decimal separator. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   value : float := 22.41;
begin
   Put_Line("Value = " & float'image(value));
end Welcome;

A floating-point number can also include an underline. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   value1 : float := 15.58_2;
   value2 : float := 22.41;
begin
   Put_Line("Value 1 = " & float'image(value1));
   Put_Line("Value 2 = " & float'image(value2));
end Welcome;

A floating-pointing number can also be written in scientific format. To represent it, use e or E. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   value1 : float := 15.58_2;
   value2 : float := 22.41;
   value3 : float := 6.05E5;
begin
   Put_Line("Value 1 = " & float'image(value1));
   Put_Line("Value 2 = " & float'image(value2));
   Put_Line("Value 3 = " & float'image(value3));
end Welcome;

This would produce:

Value 1 =  1.55820E+01
Value 2 =  2.24100E+01
Value 3 =  6.05000E+05

Here is an example that uses a declare section:

 Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   
begin
    declare
        number : Float;
    begin
        number := 248.36;

        put_line("Number = " & Float'Image(number));
    end;
end Exercise;

Specifying the Precision

with Ada.Text_IO, Ada.Float_Text_IO;
use Ada.Text_IO, Ada.Float_Text_IO; --, Example1;

procedure Exercise is
   type Natural_Number is Range 1 .. 100;
   number : Natural_Number;
   
   type Decimal_Number is digits 2;
   Single : Decimal_Number;
   
   DecNbr : Float;
   
begin
   number := 48;
   Single := 4.8;
   DecNbr := 15.528462;
   
   Put("Number = ");
   Put(Natural_Number'Image(number)); New_Line;
   Put("Decimal = ");
   Put(Decimal_Number'Image(Single)); New_Line;
   Put("Decimal = ");
   Put(DecNbr, Fore => 4, Aft => 2, Exp => 0); New_Line;
   Put("Decimal = ");
   Put(DecNbr, Fore => 4, Aft => 2, Exp => 3); New_Line;
end Exercise;

This would produce:

Number  = 48
Decimal = 4.8E+00
Decimal = 15.53
Decimal = 1.55E+01

 

Constants

A constant is a value that doesn't change. To create a constant, you use the constant keyword as follows:

VariableName : constant DataType := Value;

Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   value1, value2 : integer;
   value3 : constant integer := 605;
begin
   value1 := 15582;
   value2 := 2241;
   
   Put_Line("Value 1 = " & integer'image(value1));
   Put_Line("Value 2 = " & integer'image(value2));
   Put_Line("Value 3 = " & integer'image(value3));
end Welcome;

Any type of variable can be created as a constant, as long as you assign a value to it when declaring it. Here are example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   EmployeeNumber : constant integer := 202757;
   EmployeeName   : constant String := "Herbert Solens";

begin
   Put_Line("Employee #: " & integer'image(EmployeeNumber));
   Put_Line("Full Name:  " & EmployeeName);
end Welcome;

Once a constant has been created, you cannot assign a new value to its variable. For example, the following will produce an error:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   value1, value2 : integer;
   value3 : constant integer := 605;
begin
   value1 := 15582;
   value2 := 2241;
   value3 := 39823;
   
   Put_Line("Value 1 = " & integer'image(value1));
   Put_Line("Value 2 = " & integer'image(value2));
   Put_Line("Value 3 = " & integer'image(value3));
end Welcome;

When you are creating a constant, if its value is a number, you don't have to specify its data type. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
   EmployeeNumber : constant := 602058;

begin
   Put_Line("Employee #: " & integer'image(EmployeeNumber));
end Welcome;

 

 
 
   
 

Previous Copyright © 2010 FunctionX, Inc. Next