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

  1.                          -- Chapter 8 - Programming exercise 3
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure CH08_3 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    Int_Dat : INTEGER;
  11.    Flt_Dat : FLOAT;
  12.  
  13.  
  14.    function Raise_To_Power(Index : INTEGER) return INTEGER is
  15.    begin
  16.       Put_Line("In the INTEGER function.");
  17.       return Index * Index;
  18.    end Raise_To_Power;
  19.  
  20.  
  21.  
  22.    function Raise_To_Power(Value : FLOAT) return FLOAT is
  23.    begin
  24.       Put_Line("In the FLOAT function.");
  25.       return Value * Value * Value;
  26.    end Raise_To_Power;
  27.  
  28.  
  29.  
  30.    function Raise_To_Power(Value : INTEGER) return FLOAT is
  31.    begin
  32.       Put_Line("In the new function.");
  33.       return FLOAT(Value * Value * Value);
  34.    end Raise_To_Power;
  35.  
  36.  
  37.  
  38.    procedure Raise_To_Power(Index  : in     INTEGER;
  39.                             Result :    out INTEGER) is
  40.    begin
  41.       Put_Line("In the INTEGER procedure.");
  42.       Result := Index * Index * Index;
  43.    end Raise_To_Power;
  44.  
  45.  
  46.  
  47.    procedure Raise_To_Power(Value  : in     FLOAT;
  48.                             Result :    out FLOAT) is
  49.    begin
  50.       Put_Line("In the FLOAT procedure.");
  51.       Result := Value * Value;
  52.    end Raise_To_Power;
  53.  
  54. begin
  55.  
  56.    Int_Dat := Raise_To_Power(2);     -- uses INTEGER function
  57.  
  58.    Flt_Dat := Raise_To_Power(3);     -- uses new function
  59.  
  60.    Raise_To_Power(3,Int_Dat);        -- uses INTEGER procedure
  61.  
  62.    Raise_To_Power(2.73,Flt_Dat);     -- uses FLOAT procedure
  63.  
  64.    Int_Dat := 2;
  65.                       -- In the following statement,
  66.                       -- the function returns 2 squared, or 4
  67.                       -- and the procedure cubes it to 64.
  68.    Raise_To_Power(Raise_To_Power(Int_Dat),Int_Dat);
  69.    Put("The result is ");
  70.    Put(Int_Dat);
  71.    New_Line;
  72.  
  73. end CH08_3;
  74.  
  75.  
  76.  
  77.  
  78. -- Result of execution
  79.  
  80. -- In the INTEGER function.
  81. -- In the new function.
  82. -- In the INTEGER procedure.
  83. -- In the FLOAT procedure.
  84. -- In the INTEGER function.
  85. -- In the INTEGER procedure.
  86. -- The result is     64
  87.  
  88.