Multiple Return Values in Ada
Ada supports multiple return values through the use of out parameters. This feature is commonly used to return both result and error values from a procedure.
with Ada.Text_IO; use Ada.Text_IO;
procedure Multiple_Return_Values is
-- This procedure uses out parameters to return multiple values
procedure Vals (A : out Integer; B : out Integer) is
begin
A := 3;
B := 7;
end Vals;
A, B, C : Integer;
begin
-- Here we use the out parameters to get multiple return values
Vals (A, B);
Put_Line (Integer'Image (A));
Put_Line (Integer'Image (B));
-- If you only want a subset of the returned values,
-- you can simply ignore the others
Vals (C, B);
Put_Line (Integer'Image (B));
end Multiple_Return_Values;
To run the program, save it as multiple_return_values.adb
and use the Ada compiler:
$ gnatmake multiple_return_values.adb
$ ./multiple_return_values
3
7
7
In Ada, we use procedures with out parameters to simulate multiple return values. The Vals
procedure takes two out
parameters, which are filled with the values we want to return.
In the begin
block, we call Vals
and immediately use the returned values. Ada doesn’t have a built-in print function like fmt.Println
, so we use Ada.Text_IO.Put_Line
to output the values. We also need to convert the integers to strings using Integer'Image
.
If you only want a subset of the returned values, you can simply ignore the ones you don’t need. Ada doesn’t have a blank identifier like Go’s _
, but you can achieve the same effect by just not using one of the out parameters.
Ada’s approach to multiple return values via out parameters offers similar functionality to Go’s multiple return values, allowing for clear and efficient handling of multiple results from a single subprogram call.