Named Notation, Default Parameters & Overloading

Call subprograms with positional and named parameter association, give parameters default values, and overload subprogram names by parameter and result types — including how the compiler resolves overloads.

Ada call syntax is designed to be self-documenting. Named notation lets you write Draw_Box (Width => 4, Fill => '*') so a reader knows what every argument means, default values let callers omit rarely changed parameters, and overloading lets related operations share a clear name. This lesson shows each feature and the rules that keep them unambiguous.

Positional and Named Association

Positional association matches arguments by position: Draw_Box (6, 1). Named association matches by formal parameter name: Draw_Box (Height => 1, Width => 6), in any order. You may mix them, but positional arguments must come first. Named notation is strongly recommended when a call has several parameters of the same type or Boolean flags — Connect (Host, Port, True) says nothing, Connect (Host, Port, Use_TLS => True) says everything.

• Positional: F (1, 2). Named: F (A => 1, B => 2), any order. • Mixed: positional first, then named. • Use named notation for Booleans and same-typed parameters.

Default Parameters

An 'in' parameter can have a default expression: Height : Positive := 3. Callers may omit it; the default is evaluated at each call. Parameters with defaults are usually placed last so positional calls remain natural, and named notation lets callers skip a middle parameter while specifying a later one.

• Defaults allowed for in parameters: Param : T := Default. • Evaluated at each call. • Skip middle defaults using named notation.

Overloading

Several subprograms may share a name if their parameter and/or result type profiles differ: Describe (Integer), Describe (Float), Describe (Boolean). Ada can even overload on the result type alone — something few languages allow — because it resolves each call from the complete context. If the context does not determine a unique subprogram, the call is ambiguous and must be disambiguated, typically by qualifying an argument (Describe (Integer'(5))). Enumeration literals and operators are overloadable too.

• Same name, different parameter/result profiles. • Overloading on result type is allowed. • Ambiguity is a compile error — qualify expressions to resolve.

Code Examples

Draw_Box is called with all defaults, with named notation skipping Height, and positionally. Three Describe functions overload on the parameter type. Output: ########## ########## ########## ** ** ###### Integer 42 Float 2.50000E+00 Boolean TRUE Draw_Box (Width => 4, '*');

Draw_Box (Width => 4, Fill => '*');

Once you use named association, all following arguments must be named.

Set_Mode (True, False, True);

Set_Mode (Verbose => True, Strict => False, Color => True);

Positional Booleans are unreadable; name them.

Show (5); -- Show exists for Integer and Long_Integer Show (Integer'(5));

An integer literal fits both profiles; qualify it to choose.

Which call is legal for procedure P (A, B, C : Integer)? A. P (A => 1, 2, 3) B. P (1, C => 3, B => 2) C. P (1, B := 2, 3) D. P (C => 3, 1, 2)

B. Positional first, then named in any order.

Can Ada overload two functions that differ only in return type? A. Yes B. No C. Only operators D. Only in packages

A. Yes — overload resolution uses the full context, including expected result type.

Which parameters may have default values? A. in parameters B. out parameters C. in out parameters D. All A. in parameters.

How do you resolve an ambiguous overloaded call with a literal argument? A. Cast with (int) B. Qualify it: T'(Literal) C. Add a comment D. Impossible B. Qualify it with T'(...).

Predict the output of the named-notation example.

Real output: ########## ########## ########## ** ** ###### Integer 42 Float 2.50000E+00 Boolean TRUE

with Ada.Text_IO; use Ada.Text_IO;

procedure L16_Named is

   procedure Draw_Box (Width  : Positive := 10;
                       Height : Positive := 3;
                       Fill   : Character := '#') is
   begin
      for Row in 1 .. Height loop
         Put_Line ([1 .. Width => Fill]);
      end loop;
   end Draw_Box;

   --  Overloading: same name, different parameter or result types
   function Describe (X : Integer) return String is ("Integer" & Integer'Image (X));
   function Describe (X : Float) return String is ("Float" & Float'Image (X));
   function Describe (X : Boolean) return String is ("Boolean " & Boolean'Image (X));
begin
   Draw_Box;                                   --  all defaults
   Draw_Box (Width => 4, Fill => '*');         --  named notation, skip Height
   Draw_Box (6, 1);                            --  positional
   Put_Line (Describe (42));
   Put_Line (Describe (2.5));
   Put_Line (Describe (True));
end L16_Named;