Declare Blocks
Declare Blocks
- Declare blocks provide capability to declare variables anywhere, like Java and to allocate
dynamically sized variables
- Example:
procedure declare_example is
size: natural;
begin
get(size);
declare
a: array(1 .. size) of Integer;
-- Array a is dynamically allocated of length size
begin
for i in a'range loop
get(a(i));
end loop;
for i in a'range loop
put(a(i));
end loop;
end;
end declare_example;
Declare Blocks Can be Labeled
- Declare blocks provide capability to declare variables anywhere, like Java and to allocate
dynamically sized variables
- Example:
procedure declare_example is
size: natural;
begin
get(size);
array_block: declare
a: array(1 .. size) of Integer;
-- Array a is dynamically allocated of length size
begin
for i in a'range loop
get(a(i));
end loop;
for i in a'range loop
put(a(i));
end loop;
end array_block;
end declare_example;
Unconstrained Array Types and Declare Blocks
- Unconstrained array types and declare blocks are useful:
get(n);
declare
s: String(1 .. n);
begin
put(s);
end;
Declare blocks can be used in a loop!
...
someloop: loop
someblock: declare
s: String := ada.text_io.get_line; -- Return type?
-- s is dynamically sized each loop pass!
begin
put_reverse(s(2 .. s'last));
-- print in reverse all characters of s, except the first
-- Can also do this:
put_reverse(get_line);
end someblock;
end someloop;
s is allocated on the stack, not the heap - No garbage created!
Declare Blocks and Unconstrained Arrays
- Unconstrained arrays allow definition of array types whose index range is
assigned later
- The example below is similar to the example using Strings, above, but it has a user-defined
unconstrained type instead of type String
- Example:
with ada.text_io; with ada.integer_text_io;
use ada.text_io; use ada.integer_text_io;
procedure declare_example2 is
type My_Array_Type is array(Natural range <>) of Integer;
size: natural;
begin
get(size);
declare
a: My_Array_Type(1 .. size);
begin
for i in a'range loop
get(a(i));
end loop;
for i in a'range loop
put(i);
end loop;
end;
-- put(a'last); -- Error. Out of scope
end declare_example2;
Begin - End
begin
- end
is similar to
{}
- Any
begin-end
block can contain other
begin-end
blocks
-- Demonstrates using a begin-end block.
-- The extra block has no purpose in the program.
procedure anywhere1 is
i: integer := 1;
j: Integer := i;
begin
i := 2 * i;
begin
j := 3 * j;
put(j);
new_line;
end;
end anywhere1;
Begin-end blocks are used for
- Declare blocks: Provides capability to declare variables anywhere, like Java and to allocate
dynamically sized variables
- Exceptions: Like Java
try-catch
Begin/End Blocks
- Begin/end blocks can be added to allow for exception handling
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure eofloop is
i: Integer;
begin
while not ada.text_io.end_of_file loop
begin
get(i);
put(i);
new_line;
exception
when data_error => put_line("You must enter a number ");
when constraint_error => put_line("You must enter a natural number");
end
end loop;
end eofloop;
Like java try/catch blocks
Can also use a declare with a begin/end block (see below)