28 February 2009

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);



0 comments:

Post a Comment

Powered by Blogger.

About Me

Featured Post

5G Network Simulation in NS3 using mmWave | NS3 Tutorial 2024

5G Network Simulation in NS3 Using mmWave This post shows the installation of ns3mmwave in Ubuntu 24.04 and simulates 5G networks in ns3. In...

Contact form

Name

Email *

Message *

Total Pageviews

Search This Blog

Pages

Pages

Pages - Menu

Most Popular

Popular Posts