what is the main Difference between out mode and in out mode?

1.9k Views Asked by At

Difference between out mode and in out mode? As far i have collected the info is the main difference i know is that in both out and in out mode the actual parameter is expected to be altered even writing and reading is also possible then what is main difference please help me to understand?

1

There are 1 best solutions below

0
On

The best explanation of the difference is in RM 6.4.1(11-15).

Semantically, there is a difference only for parameters that are passed by copy. If you call a subprogram that has an in out parameter, the actual parameter is expected to have a legitimate value before the call. On entry, the subprogram makes a copy of the variable and checks to make sure it satisfies the constraints.

For an out parameter, the actual parameter does not need to have a value going in; it could be uninitialized garbage. The intent is that the subprogram will not use the value of this parameter until it has been set, by the subprogram (it can be set either by assignment or by being passed as an out parameter to some other subprogram, or to a Default_Value, or perhaps by other means). This is not enforced, however.

This can cause different behavior in some cases. For example:

subtype Int10 is Integer range 1 .. 10;

procedure Proc (Param : in out Int10) is 
begin
    Param := 5;
end Proc;

Y : Integer := 100;
...
Proc (Y);

Since Param is an in out parameter, the constraints are checked on entry. Thus, the call to Proc(Y) raises a Constraint_Error. However:

subtype Int10 is Integer range 1 .. 10;

procedure Proc (Param : out Int10) is 
begin
    Param := 5;
end Proc;

Y : Integer := 100;
...
Proc (Y);

In this case, no Constraint_Error is raised. In fact, Y does not need to be initialized at all, because the expectation is that the procedure will not use the input value. According to the RM, the value is not even copied, for certain classes of parameter types. So in this case:

Save_Param : Integer;

procedure Proc (Param : out Int10) is 
begin
    Save_Param := Param;  -- legal but not recommended
    Param := 5;
end Proc;

Y : Integer := 3;
...
Proc (Y);

Save_Param will probably be set to some garbage value, not 3. (In Ada 2012, there is a Default_Value aspect that can be applied to subtypes; in a case like that, an out parameter would be set to this default value instead of uninitialized garbage, while an in out parameter would still get the value from the actual parameter.)

For parameters that are passed by reference, there really is no difference in behavior.

The rules in Ada 83 were different. A subprogram with an in out parameter could both read and write that parameter, while a subprogram with an out parameter was allowed to assign to the parameter but could not do anything that would read that parameter's value (barring some cases where there was no way around reading discriminants). Thus:

procedure Proc (Param : out Int10) is 
begin
    Param := 5;
    Save_Param := Param;  -- illegal in Ada 83, legal in Ada 95 and later versions
end Proc;

In other words, an out parameter really was output-only. The rule was relaxed in Ada 95, though, as programmers found it too restrictive.