3.7 Block Statements

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

A block statement is a compound statement used to "localize" entities that are used in a very limited part of the enclosing unit. The entities may be declared objects or exception handlers. Some block statements are referred to as "declare blocks." The reserved words begin and end always appear. The reserved word declare often appears, but may not when the word exception and an exception handler are present.

General Form

Block_Name                       -- optional
  declare                        -- optional
    -- declaration part
    -- statement_sequence
  exception                      -- optional
    -- exception_handling_sequence
  end Block_Name;

If a Block_Name is used, the name must appear again after end.

An example "declare block" is given below. It is included as part of a Sort_Array procedure, which is an alternative version of the procedure used in our earlier example of a separately compiled subunit. The motivation for such a block is to help the reader of a program, who need only scan a small portion of the program to determine how the local object is declared and the region of the program over which it has effect. In our example the enclosing procedure is quite small, so the gain in readability is small. To appreciate the potential advantage, you should imagine such a block appearing in a large, complex routine.

Source Code Listing

--  This procedure contains a block statement that localizes 
--  the variable called Temporary, used in swapping two  
--  values of the Data array.   
separate (Temperature_Array)
procedure Sort_Array(Data : in out Data_Type) is
  Tio.Put_Line("Sorting array");
  for N in reverse 1..(Data'Last - 1) loop
    for I in 1..N loop
      if Data(I) > Data(I+1) then
        declare                        -- block starts here
          Temporary : Float;
          Temporary := Data(I);
          Data(I)   := Data(I+1);
          Data(I+1) := Temporary;
        end;                           -- block ends here
      end if;  
    end loop;  
  end loop;
end Sort_Array;

Note the use of reverse in the loop iteration scheme. The next section provides an example including a block statement used to localize an exception handler.

Related Topics

2.14 Subunits 3.8 Exception Handling
A.2 Simple and Compound Statements 

[ Back to top of pagePrev ] Next ]