Packages in ADA

Benefits of Packages
  1. Packages can be compiled, debugged independently
  2. software maintenance is very easier
  3. Once package is developed, they can be made accessible to everyone who has access to it.
  4. packages provide more security                                                                                                            
Packages contains two parts 1) a specification and 2) a body
Specification interfaces to the outside world.
body contains the executable statements associated with the functions

Examples of packages includ, COMPLEX NUMBERS Calculations, Creation of TIMER and CLOCK using packages and made as a library

Example
//Package specification describes the functions and variables relevant to complex number and their addition and subtraction operations

package COMPLEX_NUMBER is
type COMPLEX is 
record
 real, imag:float;
end record;
function ADD(a,b:COMPLEX) return COMPLEX;
function SUB(a,b:COMPLEX) return COMPLEX;
end COMPLEX_NUMBER;

//Paackage body will implement the function specified in the package specification

package body  COMPLEX_NUMBER is
with ROOTS; //ROOTS is also a package and now used as alibrary
use ROOTS;
with TRIGONO;
use TRIGONO;

begin 
function ADD(a,b:COMPLEX) return COMPLEX is 
return((a.real+b.real,a.imag+b.imag));
end ADD;
function SUB(a,b:COMPLEX) return COMPLEX is 
return((a.real-b.real,a.imag-b.imag));
end SUB;

end COMPLEX_NUMBER;

//Package COMPLEX_NUMBER made available as a library

with COMPLEX_NUMBER;  //retrieve the COMPLEX_NUMBER package
package CMPLXUSE is 
use COMPLEX_NUMBER; //make package available to the procedure
a:COMPLEX:=(2,3);
b:COMPLEX:=(2,3);
c:COMPLEX;
begin 
c:=ADD(a,b);
c:=SUB(a,b);
end CMPLXUSE;


Comments

Popular Posts