Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Subprograms and Packages

Procedures, Functions and Parameter Modes in Ada

Write Ada subprograms: procedures versus functions, the parameter modes in, out and in out, default and named parameters, expression functions and how Ada passes arguments safely.

Ada has two kinds of subprogram. A procedure performs an action and has no result; a function computes a value and returns it. The distinction is enforced: a function call is an expression, a procedure call is a statement. What makes Ada subprograms special is the parameter mode: each parameter says whether it is read, written or both, and the compiler checks the promise.

Ada listings are read-only on this site; stand-ins run in C.

Procedures

ADA
procedure Greet (Name : String; Times : Positive := 1) is
begin
   for I in 1 .. Times loop
      Put_Line ("Hello, " & Name);
   end loop;
end Greet;

A procedure is called as a statement: Greet ("Ada"); or Greet (Name => "Ada", Times => 3);. The second form uses named association, which makes calls self-documenting and lets you skip parameters that have defaults.

Functions

ADA
function Square (X : Integer) return Integer is
begin
   return X * X;
end Square;

function Is_Even (N : Integer) return Boolean is (N mod 2 = 0);   --  expression function

A function body must end every path with a return. All function parameters in classic Ada are in parameters: a function cannot change its arguments (Ada 2012 relaxes this for in out in special cases, but the rule of thumb stands). A function call is an expression: Total := Square (4) + Square (5);.

An expression function (Expr) is a one-line function whose body is an expression. It is concise and, in SPARK, easy to reason about.

The three parameter modes

Mode Meaning Rough C equivalent
in read-only (the default) pass by value, or const T *
out callee must assign it; caller gets the result pass pointer to uninitialized output
in out read and modified pass pointer, reads and writes
ADA
procedure Swap (A, B : in out Integer) is
   Temp : constant Integer := A;
begin
   A := B;
   B := Temp;
end Swap;

procedure Divide (Dividend, Divisor : Integer;
                  Quotient, Remainder : out Integer) is
begin
   Quotient  := Dividend / Divisor;
   Remainder := Dividend rem Divisor;
end Divide;

The compiler rejects assigning to an in parameter and warns if an out parameter is never assigned. For scalars the compiler may pass by copy; for large records and arrays it passes by reference. The language guarantees the visible behavior, so you rarely need to think about it. You never write & or * at the call site: Swap (X, Y);.

flowchart LR
    C["caller variable"] -->|"in: value read"| P["procedure"]
    P -->|"out: result written"| C
    C <-->|"in out: read and write"| P

The mode states the direction of data flow, which readers and the compiler can both rely on.

Named and default parameters

Call a subprogram positionally, by name, or mixed (positional first):

ADA
Draw (10, 20);
Draw (X => 10, Y => 20, Color => Red);
Draw (10, 20, Color => Red);

A parameter with a default (Color : Color_Type := Black) may be omitted. Because named association is checked, reordering parameters in a later version does not silently change meaning.

Returning unconstrained values

A function may return an array or string whose size is only known at the call:

ADA
function Repeat (S : String; N : Natural) return String is
   Result : String (1 .. S'Length * N);
begin
   for I in 0 .. N - 1 loop
      Result (I * S'Length + 1 .. (I + 1) * S'Length) := S;
   end loop;
   return Result;
end Repeat;

There is no malloc and no caller-allocated buffer; the compiler manages the result, usually on the secondary stack.

Stand-in: parameter modes in C

C passes everything by value, so in out and out use pointers. The stand-in below shows Swap and Divide and the discipline Ada enforces for you: the output parameters are always written.

C Stand-in: out and in out parameters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C has no named association, but designated initializers on a parameter struct get close. This stand-in mimics Draw (X => 10, Color => Red) with defaults:

C Stand-in: named parameters with defaults

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Common mistakes

  • Trying to modify an in parameter: the compiler refuses.
  • Forgetting a return on some path of a function.
  • Using a function for an action with side effects; prefer a procedure.
  • Not assigning an out parameter on every path.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement is true about a procedure and a function?

    Functions return a value and are used in expressions; procedures are called as statements.
  2. 2. What does the in out mode mean?

    in out parameters are both inputs and outputs.
  3. 3. What is the default parameter mode?

    If you write no mode, the parameter is in (read-only).
  4. 4. What is the benefit of named association like Draw (X => 10, Y => 20)?

    Names make the intent of each argument visible and let defaults be skipped.

Summary

  • Procedures are statements; functions return values and are expressions.
  • Parameter modes in, out and in out state the direction of data flow and are enforced.
  • Named association and defaults make calls readable; expression functions are one-liners.
  • Functions can return arrays and strings of any size.

Next: Packages: Specification and Body