Simple Function 2
MC logo
 

Simple Function 2

Ada Code Examples


<<Simple Function 1 Download Operator Overloading>>
--
-- Function using variable-sized array parameter.
--
with Gnat.Io; use Gnat.Io;
procedure f2 is
   -- An integer array.
   type IntArr is array (Integer range <>) of Integer;

   -- Compute the max of the array.
   function Max (Arr: IntArr) return Integer is
      TheMax: Integer := Arr(Arr'First);
      I: Integer;
   begin
      for I in Arr'First + 1 .. Arr'Last loop
         if Arr(I) > TheMax then
            TheMax := Arr(I);
         end if;
      end loop;
      return TheMax;
   end;

   -- Some random arrays.
   A: IntArr(1..7) := (4, 5, 12, 4, 71, -9, 21);
   B: IntArr(-3..3) := (-4, -21, -3, -18, -7, -29, -10);
begin
   Put(Max(A));
   Put(" ");
   Put(Max(B));
   Put(" ");
   B(-2) := 10;
   Put(Max(B));
   Put(" ");
   Put(Max((2, 4, 21)));
   New_Line;
end f2;
<<Simple Function 1 Operator Overloading>>