So far, we have used procedures, mostly to have a main body of code to execute. Procedures are one kind of subprogram.
There are two kinds of subprograms in Ada, functions and procedures. The distinction between the two is that a function returns a value, and a procedure does not.
This example shows the declaration and definition of a function:
Subprograms in Ada can, of course, have parameters. One syntactically important note is that a subprogram which has no parameters does not have a parameter section at all, for example:
procedure Proc; function Func return Integer;
Here's another variation on the previous example:
In this example, we see that parameters can have default values. When calling the subprogram, you can then omit parameters if they have a default value. Unlike C/C++, a call to a subprogram without parameters does not include parentheses.
This is the implementation of the function above:
We can then call our subprogram this way:
Ada allows you to name the parameters when you pass them, whether they have a default or not. There are some rules:
- Positional parameters come first.
- A positional parameter cannot follow a named parameter.
As a convention, people usually name parameters at the call site if the function's corresponding parameters has a default value. However, it is also perfectly acceptable to name every parameter if it makes the code clearer.
As briefly mentioned earlier, Ada allows you to declare one subprogram inside of another.
This is useful for two reasons:
- It lets you organize your programs in a cleaner fashion. If you need a subprogram only as a "helper" for another subprogram, then the principle of localization indicates that the helper subprogram should be declared nested.
- It allows you to share state easily in a controlled fashion, because the nested subprograms have access to the parameters, as well as any local variables, declared in the outer scope.
For the previous example, we can move the duplicated code (call to
Put_Line) to a separate procedure. This is a shortened version with
An important feature of function calls in Ada is that the return value at a call cannot be ignored; that is, a function call cannot be used as a statement.
If you want to call a function and do not need its result, you will still need to explicitly store it in a local variable.
In the GNAT toolchain
In GNAT, with all warnings activated, it becomes even harder to ignore the result of a function, because unused variables will be flagged. For example, this code would not be valid:
function Read_Int (Stream : Network_Stream; Result : out Integer) return Boolean; procedure Main is Stream : Network_Stream := Get_Stream; My_Int : Integer; B : Boolean := Read_Int (Stream, My_Int); -- Warning here, B is never read begin null; end Main;
You then have two solutions to silence this warning:
- Either annotate the variable with pragma Unreferenced, thus:
B : Boolean := Read_Int (Stream, My_Int); pragma Unreferenced (B);
- Or give the variable a name that contains any of the strings
So far we have seen that Ada is a safety-focused language. There are many ways this is realized, but two important points are:
- Ada makes the user specify as much as possible about the behavior expected for the program, so that the compiler can warn or reject if there is an inconsistency.
- Ada provides a variety of techniques for achieving the generality and flexibility of pointers and dynamic memory management, but without the latter's drawbacks (such as memory leakage and dangling references).
Parameters modes are a feature that helps achieve the two design goals above. A subprogram parameter can be specified with a mode, which is one of the following:
||Parameter can only be read, not written|
||Parameter can be written to, then read|
||Parameter can be both read and written|
The default mode for parameters is
in; so far, most of the examples
have been using
Functions and procedures were originally more different in philosophy. Before Ada 2012, functions could only take "in" parameters.
The first mode for parameters is the one we have been implicitly using so far. Parameters passed using this mode cannot be modified, so that the following program will cause an error:
The fact that this is the default mode is in itself very important. It means that a parameter will not be modified unless you explicitly specify a mode in which modification is allowed.
In out parameters¶
To correct our code above, we can use an "in out" parameter.
An in out parameter will allow read and write access to the object passed as parameter, so in the example above, we can see that A is modified after the call to Swap.
While in out parameters look a bit like references in C++, or regular parameters in Java that are passed by-reference, the Ada language standard does not mandate "by reference" passing for in out parameters except for certain categories of types as will be explained later.
In general, it is better to think of modes as higher level than by-value versus by-reference semantics. For the compiler, it means that an array passed as an in parameter might be passed by reference, because it is more efficient (which does not change anything for the user since the parameter is not assignable). However, a parameter of a discrete type will always be passed by copy, regardless of its mode (which is more efficient on most architectures).
The "out" mode applies when the subprogram needs to write to a parameter that might be uninitialized at the point of call. Reading the value of an out parameter is permitted, but it should only be done after the subprogram has assigned a value to the parameter. Out parameters behave a bit like return values for functions. When the subprogram returns, the actual parameter (a variable) will have the value of the out parameter at the point of return.
In other languages
Ada doesn't have a tuple construct and does not allow returning multiple values from a subprogram (except by declaring a full-fledged record type). Hence, a way to return multiple values from a subprogram is to use out parameters.
For example, a procedure reading integers from the network could have one of the following specifications:
procedure Read_Int (Stream : Network_Stream; Success : out Boolean; Result : out Integer); function Read_Int (Stream : Network_Stream; Result : out Integer) return Boolean;
While reading an out variable before writing to it should, ideally, trigger an error, imposing that as a rule would cause either inefficient run-time checks or complex compile-time rules. So from the user's perspective an out parameter acts like an uninitialized variable when the subprogram is invoked.
In the GNAT toolchain
GNAT will detect simple cases of incorrect use of out parameters. For example, the compiler will emit a warning for the following program:
Forward declaration of subprograms¶
As we saw earlier, a subprogram can be declared without being fully defined, This is possible in general, and can be useful if you need subprograms to be mutually recursive, as in the example below: