Previous: Variable Declarations To the Table of Content Next: Expressions

- 2.2.7.5 -
TMT Pascal Language Description
Pascal Language Structure
Declarations

Local Block Declarations

It is very often necessary to declare a local variable with a short life-span. One has to do this at the declaration part of a program or in the procedure body. This is not always convenient, especially if there is a huge program with a complicated algorithm. For that case a special construction has been added in TMT Pascal. It is called a Local or Nested Block. Such a block is an ordinary compound statement which begins with the new reserved word declare and consists of two parts - declaration and execution:
declare
   <declaration part>
begin
  <execution part>
end;
This statement can be used in any place where a structured statement can be placed.

Example:
program DeclDemo;
var
  b: Integer;
begin
  declare
  // first local block
  var
    a: Integer;
    procedure pr_int(a: Integer);
    var
      i: Integer;
    begin
      for i := 1 to a do
        declare
         //second local block
        var
          k: Integer;
        begin
          k := a div i;
          Writeln(a, ' div ', i, ' =  ', k);
        end;
    end;
  begin
    a := 1;
    Writeln(a);
    b := 10;
    pr_int(b);
  end
end.
This example contains two local blocks one of them is in the program body and another is in the routine body. The first local block declares variable a and procedure pr_int, the second declares one local variable k. It should be understood that the scope of 'a' and pr_int is the interior of the "first local block," and the scope of k is the interior of the "second local block."


Previous: Variable Declarations To the Table of Content Next: Expressions
Variable Declarations Table of Content Expressions

- 2.2.7.5 -