ZetCode

Ada procedures and functions

last modified November 23, 2026

A subprogram gives a name to a piece of work and lets it run more than once. Ada has two kinds: a procedure performs an action and is called as a statement, while a function returns a value and is called inside an expression. Parameters carry values in and out, may have defaults, and may be written by name. This article builds subprograms from the simplest procedure to overloading and recursion.

Procedures with parameters

A procedure is declared in the declarative part of another subprogram or package and is called by its name followed by the arguments in parentheses. A parameter without a mode keyword has mode in, which means the caller supplies a value and the body reads it. The body of the procedure has its own declarative part and its own begin and end.

greet.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Greet is
   procedure Say_Hello (Name : String) is
   begin
      Put_Line ("Hello there, " & Name & "!");
   end Say_Hello;
begin
   Say_Hello ("Ada");
   Say_Hello ("GNAT");
end Greet;

The nested procedure is visible from the statement part of Greet and is called twice with different strings. The parameter Name is a constant inside Say_Hello, so an assignment to it would be rejected. Moving the procedure outside the main program would make it a library unit in its own file, which the packages article later in the series develops.

$ gnatmake -gnat2022 -gnatwa greet.adb
$ ./greet
Hello there, Ada!
Hello there, GNAT!

Functions

A function writes return and a type after its parameter list, and its body must produce a value of that type on every path. The call may stand anywhere an expression of the return type is allowed, including as an argument to another call. Unlike a procedure, a function cannot be called as a statement; its result is always used.

area_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

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

   Side : constant Integer := 6;
begin
   Put_Line ("area =" & Integer'Image (Square (Side)));
end Area_Demo;

Square takes one integer and returns its product with itself, so the area of a square with side 6 is 36. The body contains a single return statement, but a function may have several, as long as each path through the body ends in one. The compiler checks this rule and reports a missing return rather than letting the function fall off its end.

$ gnatmake -gnat2022 -gnatwa area_demo.adb
$ ./area_demo
area = 36

out parameters

A parameter with mode out is a result rather than an input: the body assigns it, and the caller must pass a variable to receive the value. Inside the subprogram an out parameter starts without a defined value, so it must be assigned before it is read. A procedure may return several results this way, which is why Ada does not need a tuple type for the purpose.

split_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Split_Demo is
   procedure Divide (Value     : Integer;
                     Quotient  : out Integer;
                     Remainder : out Integer) is
   begin
      Quotient  := Value / 10;
      Remainder := Value mod 10;
   end Divide;

   Q : Integer;
   R : Integer;
begin
   Divide (47, Q, R);
   Put_Line ("quotient =" & Integer'Image (Q));
   Put_Line ("remainder =" & Integer'Image (R));
end Split_Demo;

Divide splits 47 into a quotient of 4 and a remainder of 7. Q and R are declared without initial values, because the procedure fills them before they are read. Passing a literal or an expression as an out argument would be rejected: the compiler needs a variable that can be updated.

$ gnatmake -gnat2022 -gnatwa split_demo.adb
$ ./split_demo
quotient = 4
remainder = 7

in out parameters

The mode in out combines both directions: the body reads the current value, changes it, and the caller sees the new value when the call returns. The classic use is a swap, which needs a temporary inside the subprogram to avoid losing one of the two values. Both arguments must be variables of the declared parameter type.

swap_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Swap_Demo is
   procedure Exchange (Left  : in out Integer;
                       Right : in out Integer) is
      Temp : constant Integer := Left;
   begin
      Left  := Right;
      Right := Temp;
   end Exchange;

   A : Integer := 1;
   B : Integer := 2;
begin
   Exchange (A, B);
   Put_Line ("a =" & Integer'Image (A));
   Put_Line ("b =" & Integer'Image (B));
end Swap_Demo;

Before the call A is 1 and B is 2; afterwards the values are reversed. The temporary is a constant because it is initialized once and never changes. The parameter names Left and Right are local to Exchange, so they do not conflict with the caller's names, and the call associates them by position.

$ gnatmake -gnat2022 -gnatwa swap_demo.adb
$ ./swap_demo
a = 2
b = 1

Default and named parameters

A parameter may carry a default value written after :=, and a call may then omit it. When several parameters have defaults, a call can name the ones it wants to supply, using Name => Value; the positional arguments come first, and the named ones may appear in any order. Defaults make a subprogram convenient for the common case without hiding the full form.

greetings.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Greetings is
   procedure Greet (Name        : String;
                    Punctuation : String   := "!";
                    Times       : Positive := 1) is
   begin
      for I in 1 .. Times loop
         Put_Line ("Hello there, " & Name & Punctuation);
      end loop;
   end Greet;
begin
   Greet ("Ada");
   Greet ("GNAT", Times => 2);
   Greet (Name => "Bob", Punctuation => "?");
end Greetings;

The first call uses both defaults, the second overrides only the number of repetitions, and the third supplies the name and the punctuation by name while leaving Times at 1. A default is evaluated at the point of the call, rather than once when the subprogram is declared. A call must not supply the same parameter twice, once by position and once by name.

$ gnatmake -gnat2022 -gnatwa greetings.adb
$ ./greetings
Hello there, Ada!
Hello there, GNAT!
Hello there, GNAT!
Hello there, Bob?

Expression functions

A function whose body is a single expression can be written with is followed by the expression in parentheses, without begin, return, or end. This form was added in Ada 2012 and is called an expression function. It suits short computations and keeps a long list of small helpers readable, since each one occupies a few lines.

expression_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Expression_Demo is
   function Double (X : Integer) return Integer is (X * 2);

   function Max_Of (A, B : Integer) return Integer is
     (if A >= B then A else B);
begin
   Put_Line ("double =" & Integer'Image (Double (21)));
   Put_Line ("max =" & Integer'Image (Max_Of (3, 9)));
end Expression_Demo;

Double returns 42 for 21, and Max_Of returns 9. The second definition uses the if expression from the conditionals article as its body; the parentheses around the expression are part of the syntax. An expression function may be declared where a full body would be too much ceremony, and it obeys the same rules: the expression must have the return type and must be evaluated on every call.

$ gnatmake -gnat2022 -gnatwa expression_demo.adb
$ ./expression_demo
double = 42
max = 9

Overloading

Overloading gives several subprograms the same name with different parameter types. The compiler chooses the right one from the types of the actual parameters, so a program can use one verb for several kinds of value without inventing names such as Describe_Integer. The return type does not take part in the choice, which is why two functions that differ only in their return type cannot share a name.

overload_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Overload_Demo is
   function Describe (X : Integer) return String is
     (if X >= 0 then "non-negative integer" else "negative integer");

   function Describe (X : Float) return String is
     (if X >= 0.0 then "non-negative float" else "negative float");

   function Describe (X : Boolean) return String is
     (if X then "true boolean" else "false boolean");
begin
   Put_Line (Describe (42));
   Put_Line (Describe (2.5));
   Put_Line (Describe (True));
end Overload_Demo;

The three calls are identical in form and differ only in the type of the argument, so each one selects a different body. The literal 42 has type Integer, 2.5 has type Float, and True has type Boolean. Ordinary operators such as + and & are overloaded in the same way, which is why + works for both integers and reals.

$ gnatmake -gnat2022 -gnatwa overload_demo.adb
$ ./overload_demo
non-negative integer
non-negative float
true boolean

Recursion

A subprogram may call itself, provided the call moves toward a case that does not recurse. That case is the base case, and without it the chain of calls never ends. The factorial of N is the product of all integers from 1 to N, with 0! defined as 1, so the function can return 1 for N of 0 or 1 and otherwise multiply N by the factorial of N - 1.

factorial_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Factorial_Demo is
   function Factorial (N : Natural) return Natural is
   begin
      if N <= 1 then
         return 1;
      else
         return N * Factorial (N - 1);
      end if;
   end Factorial;
begin
   for N in 0 .. 5 loop
      Put_Line (Integer'Image (N) & "! =" &
                Integer'Image (Factorial (N)));
   end loop;
end Factorial_Demo;

The loop prints the factorials from 0 to 5, ending with 120. Each call decreases its argument by one, so the recursion reaches 1 after N steps and then returns through the waiting multiplications. The parameter and the result both use Natural, which rules out a negative argument; a very large N would still overflow Integer long before the stack ran out, which is why factorial examples stay small.

$ gnatmake -gnat2022 -gnatwa factorial_demo.adb
$ ./factorial_demo
 0! = 1
 1! = 1
 2! = 2
 3! = 6
 4! = 24
 5! = 120

Next steps

The loops article repeats the statements that these subprograms contain, and conditionals explains the branches inside them. The next article, packages, moves subprograms out of the main program into a specification and a body that other units can use.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have declared procedures with in parameters, returned values from functions, produced several results through out parameters, modified the caller's variables with in out parameters, used default and named associations, abbreviated short functions as expressions, overloaded one name for three types, and computed factorials recursively. The previous article covers the repetitions inside these bodies, and the next article collects subprograms into packages.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.