Ada has no parameter mode literally named "value-result." The closest standard construct is a parameter of mode out whose value the caller pre-initializes and whose final state the caller relies on: the subprogram treats it as an output, but the caller reads the post-call value as if it came back in. Since Ada 2012 you can also declare a formal parameter out and still read its incoming value inside the body by writing X'Old, which is the language's direct expression of value-result semantics. For older Ada versions, you emulate the mode by passing an in out parameter and documenting the intended copy-in/copy-out contract, or by passing the value in as an in parameter and returning a new value with a function.

The distinction matters because Ada's parameter modes are defined by data flow direction, not by the copy mechanism used by the compiler. A parameter of mode in may legally be passed by reference if the compiler can prove the body never writes to it; out and in out parameters may also be passed by reference. So "pass by value-result" in Ada is primarily a specification-level promise about which direction information travels, and only secondarily a question of memory copies.

What Value-Result Actually Means in Ada Terms

Value-result (also called copy-in/copy-out) describes a calling convention in which the actual argument's value is copied into the formal parameter at call entry, the body works on that private copy, and the formal's final value is copied back to the actual argument at return. The caller sees the original value on entry to the body only if the body reads it, and always sees the body's final assignment after the call.

Ada's three parameter modes map onto this conceptual space unevenly:

in parameters are read-only inside the body and cannot be assigned. out parameters are write-only by default; reading an out parameter before assigning it is a bounded error in the absence of 'Old, and the compiler is not required to preserve the incoming value. in out parameters are both readable and writable; the incoming value is guaranteed available to the body.

The subtlety is that out parameters in Ada are not required to start with the caller's value. The language explicitly permits the implementation to leave an out parameter uninitialized at entry, so reading it without 'Old is a programming error even though many compilers happen to copy the incoming bits. That is the gap that Ada 2012's 'Old attribute closes: X'Old denotes the value of X at the point of the call, making the copy-in half of value-result well-defined and portable.

A useful mental model is a two-column contract. The caller supplies a value, the body is allowed to inspect or ignore it, and the body's last assignment to the formal is what the caller receives back. The formal parameter acts as a private variable that starts life as a copy of the actual and dies by writing its final state back.

The Core Mechanism: Copy-In, Work, Copy-Out

The copy-in/copy-out sequence is easiest to reason about as four phases.

1: Evaluation and copy-in. The actual argument's value is evaluated (or its location identified, if passed by reference) and made available to the formal as the entry value. In Ada this is guaranteed for in and in out, and guaranteed read access inside the body via 'Old for out in Ada 2012 and later. 2: Body execution. The subprogram reads and writes the formal according to its mode. The formal is the body's view of the parameter; assignments to it do not affect the caller until return. 3: Copy-out at return. When the body completes normally, the formal's final value is written back to the actual argument's location. If the body raises an exception, Ada does not guarantee the copy-out occurred; the actual's value is unspecified for out and in out parameters after an unhandled exception propagates. 4: Caller observes the result. The caller reads the actual argument and sees the post-call value.

The failure points are worth naming explicitly. Aliasing is the classic hazard: if the actual argument is passed twice under two different names, or if the formal is aliased to a global the body also writes, the copy-out order becomes observable and the result can be implementation-dependent. Ada's rules on aliasing (RM 6.4.1) restrict some of these cases, but an in out or out parameter that is also reachable through another path is a bug waiting to happen. Second, exception propagation skips the copy-out, so a caller that catches an exception cannot rely on the "result" of an out parameter. Third, a bounded error arises if an out parameter is read before assignment without 'Old; the program is erroneous and may behave differently across compilers.

Ada 2012 and Later: The 'Old Attribute

Ada 2012 introduced the 'Old attribute specifically to give out parameters well-defined incoming values. The syntax is simple: inside the body of a subprogram, write Param'Old to obtain the value of Param at the point of the call. The attribute is legal only in postconditions and in the body of a subprogram for out and in out parameters; it is not a general-purpose attribute on arbitrary objects.

The canonical value-result pattern looks like this:

Here Total is an out parameter, the body reads its incoming value through 'Old, adds the increment, and writes the result back. The postcondition states the copy-out contract precisely. The caller must still initialize the actual argument before the call; passing an uninitialized variable as an out actual is legal syntactically but reading it via 'Old inside the body yields whatever value the caller's variable held, which is indeterminate if never assigned. That is the caller's responsibility, not the subprogram's.

The 'Old attribute also appears in preconditions and postconditions on in out parameters, where it is sometimes confused with value-result semantics. For an in out parameter, 'Old refers to the entry value for use in contracts; the parameter itself is already readable and writable in the body without 'Old. The attribute's real value is for out parameters, where it is the only standard way to recover the copy-in value.

Step-by-Step: Writing a Value-Result Subprogram

The procedure below follows Ada's preferred style for a copy-in/copy-out operation. I use a simple "clamp into range" contract, a common pattern where the caller supplies a candidate value and receives a corrected one.

Prerequisites. You need an Ada 2012 or later compiler (GNAT, for example), a package specification to hold the subprogram, and a caller that initializes the actual argument before the call. Decide up front whether the incoming value matters; if it does not, plain out without 'Old is simpler and signals "output only" to readers.

Step 1: Declare the parameter mode. Use out for the result and in for any inputs the body needs alongside. If the operation must both read and write the same parameter with intermediate assignments, use in out instead and skip 'Old.

Step 2: Decide whether the body needs the incoming value. If the clamp should "keep the original if already in range," the body must read the incoming value, so use Value'Old. If the clamp should always overwrite, a plain assignment without 'Old is enough and the postcondition can be simplified.

Step 3: Write the body using 'Old for the copy-in read.

Capturing 'Old into a local constant at the top is a small readability win; it also avoids any doubt about whether the attribute can be evaluated later in the body. (It can, but the local makes the copy-in explicit.)

Step 4: Call it with an initialized actual.

Step 5: Check success. After the call, assert the postcondition manually or enable assertion checking with -gnata in GNAT so preconditions and postconditions are verified at run time during testing. Compile with warnings enabled; GNAT's -gnatwa set will flag suspicious out parameter use, and -gnatw.o specifically warns about out parameters that are read. A successful build with no out-read warnings, plus passing assertions, is the practical check that the copy-in/copy-out contract is honored.

Stop condition. If you find yourself fighting aliasing errors (the same object passed as two actuals, or an out actual that is also a global the body writes), stop and restructure the interface. Those are not compiler bugs to work around; they are contract violations that Ada intentionally leaves implementation-dependent. At that point the right move is to redesign the subprogram signature, not to suppress warnings.

Emulating Value-Result on Pre-2012 Ada

Before Ada 2012, 'Old did not exist, so out parameters were strictly write-only from the language's point of view. Two portable emulations exist.

The in out idiom. Change the formal to in out, document in the spec that the incoming value is read and the outgoing value is written, and rely on the mode's readability guarantee. This is the most faithful emulation because in out parameters are defined to be readable at entry.

The comment in the spec becomes the contract. The drawback is that nothing in the language prevents the body from leaving the parameter unchanged, so the caller cannot distinguish "kept the value" from "forgot to assign." The postcondition cannot express the intent without 'Old either.

The function-and-assignment idiom. Take the incoming value as an in parameter and return the corrected value.

The caller writes X := Clamped (X, 0, 10);. This is pure, easy to test, and contains no aliasing hazards because the actual X is read before the call and written after. It is the idiomatic recommendation when the operation is naturally functional. The only cost is verbosity at the call site and the need for the caller to remember the assignment.

A third, less common emulation uses an access-to-variable type to simulate by-reference passing, but that is strictly worse than in out for this purpose and should not be used for dress-up. It weakens the contract and invites aliasing bugs.

out Versus in out: Choosing the Right Mode

The decision between out and in out is the practical heart of "passing by value-result" in Ada, because both can implement the pattern and they differ in what the compiler and reader can assume.

Criterion out (with 'Old) in out
Incoming value readable in body Only via 'Old (Ada 2012+) Yes, directly
Incoming value guaranteed at entry Yes, via 'Old semantics Yes
Body may ignore incoming value Yes, and the compiler knows it is write-only Legal but undiscoverable from the mode
Postcondition can reference entry value Yes, via 'Old Yes, via 'Old
Aliasing risk Present if actual aliases another parameter Same
Best fit Caller-supplied candidate, body replaces it Caller-supplied state, body reads and updates it

The rule of thumb I would give: if the body genuinely never needs the incoming value except as a default to preserve, out with 'Old is the clearer interface, because the mode tells the reader "this is primarily an output." If the body reads the incoming value multiple times and updates it incrementally, in out is the better fit, and 'Old belongs in the contract, not the code.

One caveat on the table: the "aliasing risk" row is not a difference between the modes. Both out and in out parameters can be aliased to other names, and both are subject to RM 6.4.1 restrictions. The risk exists whenever an actual argument is reachable through more than one path; the mode does not remove it.

Contracts Make the Mode Honest

The single most valuable thing Ada 2012 added for value-result parameters is not the 'Old attribute by itself but the ability to state the copy-in/copy-out relationship in a postcondition. Without contracts, an out parameter's contract lives in a comment and drifts. With contracts, the compiler can check it when assertions are enabled, and the reader gets a machine-checkable specification.

A good postcondition pattern for value-result is a two-way implication: the result equals the incoming value if the incoming value satisfies the body's precondition, otherwise it equals the corrected value. For the clamp example:

Notice the use of 'Old on both sides of the implication. The contract is now self-contained: it says "preserve the incoming value when it is already valid, otherwise produce one of the boundary values." That is exactly the copy-in/copy-out promise, expressed in a form the compiler can check under -gnata, and it reads well to a human reviewer.

A practical note on contracts and out parameters: in a precondition, an out parameter may not be referenced except through 'Old, because the precondition is evaluated before the call and the parameter's body-time value does not yet exist. Some teams write preconditions on out parameters as if they described entry state, which is a common source of confusion. When in doubt, put entry-state conditions on 'Old and leave the bare name for postconditions.

Worked Example: A Value-Result Swap and Its Limits

A swap routine is the textbook value-result case, and it also exposes the aliasing limit cleanly. The naive signature is two out parameters:

Called with Swap (A, B), this copies A and B in, swaps the copies, and copies them back. It behaves exactly like a value-result swap when A and B are distinct variables.

Now call it with the same variable twice: Swap (A, A). The actual A is aliased to both formals. RM 6.4.1 does not make this automatically illegal for an out parameter pair passed as two distinct actuals of the same object, but the semantics are not something you should rely on. The copy-out order determines the result: whatever Left was assigned last wins, so A ends up holding one of the two values, and neither value is a "swap." In practice you get A = A, which is harmless, but the point is that the routine's contract does not promise anything sensible for aliased actuals, and no amount of testing will make it robust.

The function-based alternative avoids the whole class of problem:

The caller writes (A, B) := Swapped (A, B);, and aliasing is impossible because the function's inputs are read before the assignment begins. For a swap, this is the form I would choose in any new Ada 2012 code; the procedure form is worth studying because it makes the copy-in/copy-out mechanics visible, but the function form is the safer default.

The limit runs deeper than swap. Any time the body of a value-result subprogram can observe the order in which copy-out writes happen, the result depends on implementation choices the language does not standardize. In a well-designed interface, the body should treat each out parameter as an independent value and never assume a write ordering between parameters. If the data flow genuinely requires multiple outputs with ordered side effects, model them with a record return value or a dedicated state type, not with several out parameters.

Common Mistakes and How to Avoid Them

The failures below account for most of the "value-result doesn't work" reports on Ada mailing lists and Stack Overflow. Each has a nondeterministic smell that shows up in testing.

Reading an out parameter without 'Old. The code compiles but the value read is whatever the compiler left in the parameter slot, which may be the caller's value, may be garbage, and may differ between -O0 and -O2. The fix is mechanical: either use 'Old or capture the incoming value in a local constant at the top of the body. GNAT's -gnatw.o warning catches this case.

Passing an uninitialized actual to an out parameter whose body reads 'Old. The out mode does not require the caller to initialize the actual; it only forbids the callee from reading it before assignment unless 'Old is used. If 'Old is used, the caller must initialize, or the body's read yields an indeterminate value. The fix is to initialize the actual, or to reconsider whether 'Old is really needed.

Assuming copy-out happens after exceptions. A caller with an exception handler around a call to a value-result subprogram may catch the exception and then read the actual argument, expecting to see the partially computed result. Ada does not guarantee the copy-out on abnormal completion. The fix is to avoid using out or in out parameters for results that must survive exceptions; use a function that returns a record with a success flag, or pass results through a protected object.

Fighting the compiler over aliasing. Passing the same variable as two actuals, or passing an actual that is also a global the body assigns, invites the same aliasing behavior described in the swap example. The fix is a signature change, not a warning suppression. If you find yourself reading GNAT's aliasing diagnostics and reaching for a pragma, stop and redesign.

Using 'Old outside its legal contexts. 'Old is allowed in postconditions and in subprogram bodies for out and in out parameters, not on arbitrary objects. Attempting Global'Old or using 'Old in a plain expression statement is a legality rule violation. The fix is to capture the value into a local at the top of the body where the attribute is legal.

A practical verification routine covers all five: compile with assertions enabled (-gnata) and warnings on (-gnatwa), run unit tests that call each subprogram with distinct actuals and assert the postconditions, and then audit each call site for aliased actuals. If all three pass, the value-result contract is honored.

FAQ

Can Ada pass a parameter by value-result without 'Old?

Yes, by using in out and documenting the contract, or by wrapping the operation in a function that takes the incoming value as in and returns the outgoing value. The out plus 'Old form is the most explicit Ada 2012 expression, but the in out form is fully portable to Ada 95 and Ada 2005 and is often the better fit when the body reads the incoming value more than once.

Is out in Ada already copy-in/copy-out, so 'Old is redundant?

It is not redundant. out parameters are write-only by default; the language does not require the implementation to preserve the caller's value at entry, and reading before assignment is a bounded error. Many compilers do copy the incoming bits, which is why the mistake is common, but relying on that behavior is non-portable. 'Old is what makes the copy-in half well-defined in Ada 2012 and later.

Does a value-result parameter copy the value, or pass a reference?

The mode describes data flow, not memory traffic. Ada compilers may pass in out and out parameters by reference, and may pass in parameters by reference when the body cannot prove aliasing problems. For out plus 'Old, the compiler typically passes a reference (or a copy) and the 'Old read is taken from the caller's variable at entry. From the programmer's perspective the contract is "the caller's value is the entry state and the body's final assignment is the exit state," and the compiler is free to choose whichever mechanism preserves that contract.

What if the caller passes a literal or expression to an out parameter?

The language allows expressions as actuals only if the parameter is of mode in, or in some Ada 2012 contexts where the actual is not modified through an out parameter. An out parameter requires an actual argument that denotes a variable, because the copy-out needs a location to write back to. Passing a literal to an out parameter is a legality rule violation, so the compiler rejects it before any value-result semantics come into play.