home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog2 / adder4.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  1.7 KB  |  75 lines

  1.                                        -- Chapter 15 - Program 6
  2.  
  3.                  -- Interface of AdderPkg
  4. package AdderPkg is
  5.    Total : FLOAT;               -- Global variable
  6.    type MY_ARRAY is array(INTEGER range <>) of FLOAT;
  7.    procedure Add_Em_Up(In_Dat : in     MY_ARRAY;
  8.                        Sum    :    out FLOAT);
  9. end AdderPkg;
  10.  
  11.  
  12.                  -- Implementation of AdderPkg
  13. package body AdderPkg is
  14.    procedure Add_Em_Up(In_Dat : in     MY_ARRAY;
  15.                        Sum    :    out FLOAT) is
  16.    begin
  17.       for Index in In_Dat'FIRST..In_Dat'LAST loop
  18.          Total := Total + In_Dat(Index);
  19.       end loop;
  20.       Sum := Total;
  21.    end Add_Em_Up;
  22.  
  23. begin            -- Initialization section
  24.    Total := 0.0;
  25. end AdderPkg;
  26.  
  27.  
  28.  
  29.  
  30. with Text_IO;
  31. use Text_IO;
  32.  
  33. with AdderPkg;
  34. use AdderPkg;
  35.  
  36. procedure Adder4 is
  37.  
  38.    package Flt_IO is new Text_IO.Float_IO(FLOAT);
  39.    use Flt_IO;
  40.  
  41.    FIRST : constant := 2;
  42.    LAST  : constant := 7;
  43.    Sum_Of_Values : FLOAT;
  44.  
  45.    New_Array : MY_ARRAY(FIRST..LAST);
  46.  
  47.    procedure Summer(In_Dat : MY_ARRAY;
  48.                     Sum    : out FLOAT) renames AdderPkg.Add_Em_Up;
  49. begin
  50.    for Index in New_Array'FIRST..New_Array'LAST loop
  51.       New_Array(Index) := FLOAT(Index);
  52.    end loop;
  53.  
  54.    Put_Line("Call Add_Em_Up now.");
  55.    Add_Em_Up(New_Array,Sum_Of_Values);
  56.    Put("Back from Add_Em_Up, total is");
  57.    Put(Sum_Of_Values,5,2,0);
  58.    New_Line;
  59.  
  60.           -- The next three statements are identical
  61.    Add_Em_Up(New_Array,Sum_Of_Values);
  62.    AdderPkg.Add_Em_Up(New_Array,Sum_Of_Values);
  63.    Summer(New_Array,Sum_Of_Values);
  64.  
  65. end Adder4;
  66.  
  67.  
  68.  
  69.  
  70. -- Result of execution
  71.  
  72. -- Call Add_Em_Up now
  73. -- Back from Add_Em_Up, total is   27.00
  74.  
  75.