Default and Keyword Parameters.
MC logo
 

Default and Keyword Parameters.

Ada Code Examples


<<In and Out Parameters. Download Function Overloading.>>
--
-- Keyword and default parameters.
--
with Gnat.Io; use Gnat.Io;
procedure Defparm is
   -- This type is used to provide a list of subscripts to String_Fill.
   type SubList is array ( Positive range <> ) of Integer;

   -- This procedure fills up a string under the control of various
   -- options.  It can fill with blanks by default, or any other character.
   -- It can fill the whole string or a portion thereof.  It can fill with
   -- the same character, or adjacent characters.
   -- The portion can be specified in a number of ways.
   procedure String_Fill(
          Str: out String;                -- String to be filled
          Fill_With: Character := ' ';    -- Fill with this, space by default.
          Char_Step: Integer := 0;        -- Increment the fill char each time.
          Fill_Step: Integer := 1;        -- Step by this much.
          Specific_Points: SubList := (-1, -2); -- Fill these specific points.
                                          -- (Ada won't permit default () )
          Max_Fill: Integer := -1)        -- Max number of entries to fill.
                is
      I: Integer;                       -- Scanner.
      Ch: Character := Fill_With;       -- Filling character
      Filled: Integer := 0;             -- Number of places filled.
   begin
      I := Str'First;
      while I <= Str'Last loop
         exit when Max_Fill > 0 and Filled >= Max_Fill;

         -- Fill with curr fill char.
         Str(I) := Ch;

         -- Count the filling.
         Filled := Filled + 1;

         -- Go to the next fill char.
         Ch := Character'Val(Character'Pos(Ch) + Char_Step);

         -- Next position.
         I := I + Fill_Step;
      end loop;

      -- Now fill up from the specific positions.
      for I in Specific_Points'Range loop
         exit when Max_Fill > 0 and Filled >= Max_Fill;

         -- Fill with curr fill char.
         if Specific_Points(I) in Str'Range then
            Str(Specific_Points(I)) := Ch;

            -- Count the filling.
            Filled := Filled + 1;
         end if;

         -- Go to the next fill char.
         Ch := Character'Val(Character'Pos(Ch) + Char_Step);
      end loop;
   end String_Fill;

   A: String(1..20);
   B: String(1..10);
begin
   -- One positional.
   String_Fill(A);
   Put_Line("[" & A & "]");

   String_Fill(A, '@');
   Put_Line("[" & A & "]");

   String_Fill(A);
   String_Fill(A, Fill_With => 'a', Fill_Step => 2, Char_Step => 3);
   Put_Line("[" & A & "]");

   String_Fill(A, '$');
   String_Fill(A, '-', Fill_Step => 5, Specific_Points => (4, 9, 2, 11));
   Put_Line("[" & A & "]");

   String_Fill(B, '+');
   String_Fill(B(2..10), Max_Fill => 3, Fill_With => '*', Fill_Step => 2);
   Put_Line("[" & B & "]");

   String_Fill(Fill_With => 'Z', Char_Step => -1, Str => B);
   Put_Line("[" & B & "]");
end Defparm;
<<In and Out Parameters. Function Overloading.>>