Keyword and Default Parameters
Keyword Parameters
Keyword Parameters
- A version of
Ada.Integer_Text_IO.put
has these parameters:
- Item - the integer to print
- Width - how many spaces to use to print the integer
- Base - the base to print (eg base 2 or base 16)
- Normal parameter passing is positional - matched by order
- Keyword parameters can be used - matched by parameter name!
- All of the following are legal, except the last:
put(i);
put(i, 1); -- width 1
put(i, 1, 2); -- width 1, base 2
put(Item => i);
put(Item => i, Width => 1);
put(Item => i, Width => 1, Base => 2);
put(Width => 1, Item => i);
put(Width => 1, Base => 2, Item => i);
-- Mix positional and keyword. Position must be first
put(i, Width => 1);
put(i, Width => 1, Base => 2);
put(i, 1, Base => 2);
-- put(Width => 1, i); -- Error
Default Parameters
Default Parameters
- The declaration for Ada.Text_IO.put is similar to this:
procedure Put(Item : Num;
Width : Field := Default_Width;
Base : Number_Base := Default_Base);
The Width parameter has the default value Default_Width
- If put is called without a width, then the default value is used
- What is the default width?
The Base parameter has the default value Default_Base
- What is the default base?
So the following are all the same:
put(i); -- width 11, base 10
put(i, 11); -- width 11, base 10
put(i, 11, 10); -- width 11, base 10
-- Check the values
put(default_width); -- 11
put(default_base); -- 10
OLD NOTES ON Keyword, Positional, and Default Parameters
Motivation
- Concern for programmer: easier to read and write
- Reliability: mistakes less likely
Keyword Parameters
Default Parameters
- Parameters can have default values
- Example: for new_line, the default is 1
procedure new_line(Count: Positive := 1) ...
For integer_text_io, the default width is 11 (big enough for 2 billion and a sign)
procedure put(Item: Integer; width: Natural := 11) ...