Declaration Blocks



Begin - End


Declaration Blocks

 
        -- Demonstrates declaring a variable in a declaration block within a program
        with Ada.Text_IO; use Ada.Text_IO;
        with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

        procedure anywhere2 is
           i: integer := 1;

        begin
           i := 2 * i;

           declare
              j: Integer := i;
           begin
              j := 3 * j + i;
              put(j);
              new_line;
           end;

           -- put(j);  -- Compile error

        end anywhere2;
  

Declare blocks and Dynamic Allocation of Strings

 
        -- Demonstrates declaring a string variable whose size is 
        --  not known until runtime

        with Ada.Text_IO; use Ada.Text_IO;
        with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

        procedure anywhere3 is
           s: String(1..80);
           len: integer;

        begin
           get_line(s, len);
           put(s'length);
           put(len);
           -- How do we output the string that was input?

           declare
              t: String(1..len) := s(1..len); 
           begin
              put(t);
              put(t'length);
           end;

        end anywhere3;