Blocks and Procedures in ADA

Blocks
A Block in ADA contains two parts specification and a body. The specification specifies the variable used inside the block and the body executes the statement for the block

syntax
block
begin
---
---
end

  • The main purpose of blocks are information hiding.
  • A Variable declared inside a block is accessible only within the block and nowhere outside the block.
Example
var x,y:integer
begin 
  y:=0;
 for x in 1..10 loop
  block
   var x:integer;
    begin 
    for x in 1..5 loop
    y:=y+x;
   end loop;
end;
end loop;

(NB: the above example shows two variables x within the block and outside the block, The x inside the block is accessible only within that block)

Procedures and Functions
  1. Blocks are repeated whenever they are needed, but functions can be written once, called anytimes
Examples
//to define a function to show the smaller among two integers
function SMALLER (A,B: integer) return integer is 
begin
if A < B then
  return A;
else
return B;
end if;
end SMALLER;

//function calling SMALLER
D:=C+SMALLER(x,y);


Comments

Popular Posts