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

  1.                                        -- Chapter 8 - Program 7
  2. -- This is a program to return an odd square of a number.  The result
  3. --   will have the sign of the original number, and the magnitude of
  4. --   the square of the original number.  The entire program can be
  5. --   written in one line (i.e. - Result := Index * abs(Index); ), but
  6. --   the purpose of the program is to illustrate how to put various
  7. --   procedures and functions together to build a useable program.
  8. -- Various levels of nesting are illustrated along with several
  9. --   parameters being used in procedure calls.
  10.  
  11. with Text_IO;
  12. use Text_IO;
  13.  
  14. procedure OddSqre is
  15.  
  16.    Result : INTEGER;
  17.  
  18.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  19.    use Int_IO;
  20.  
  21.    function Square_The_Number(Number_To_Square : in INTEGER)
  22.                                                  return INTEGER is
  23.    begin
  24.       return Number_To_Square * Number_To_Square;
  25.    end Square_The_Number;
  26.  
  27.    procedure Square_And_Keep_Sign(Input_Value  : in INTEGER;
  28.                                   Funny_Result : out INTEGER) is
  29.  
  30.         procedure Do_A_Negative_Number(InVal  : in INTEGER;
  31.                                        OutVal : out INTEGER) is
  32.         begin
  33.            OutVal := -Square_The_Number(InVal);
  34.         end Do_A_Negative_Number;
  35.  
  36.    begin
  37.       if Input_Value < 0 then
  38.          Do_A_Negative_Number(Input_Value, Funny_Result);
  39.          return;
  40.       elsif Input_Value = 0 then
  41.          Funny_Result := 0;
  42.          return;
  43.       else
  44.          Funny_Result := Square_The_Number(Input_Value);
  45.          return;
  46.       end if;
  47.    end Square_And_Keep_Sign;
  48.  
  49. begin
  50.  
  51.    for Index in INTEGER range -3..4 loop
  52.       Square_And_Keep_Sign(Index, Result);
  53.       Put("The Odd Square of");
  54.       Put(Index,3);
  55.       Put(" is");
  56.       Put(Result);
  57.       New_Line;
  58.    end loop;
  59.  
  60. end OddSqre;
  61.  
  62.  
  63.  
  64.  
  65. -- Result of execution
  66.  
  67. -- The Odd Square of -3 is    -9
  68. -- The Odd Square of -2 is    -4
  69. -- The Odd Square of -1 is    -1
  70. -- The Odd Square of  0 is     0
  71. -- The Odd Square of  1 is     1
  72. -- The Odd Square of  2 is     4
  73. -- The Odd Square of  3 is     9
  74. -- The Odd Square of  4 is    16
  75.  
  76.