Cited run ยง6.1.1 Out Parameters , it may be easier to understand after viewing ยง6.2 Formal parameter parameters . For a parameter of type A "it is not specified whether the parameter is passed by copy or link." Implementation is free to choose. In any case, the component x value of type A does not change to P2 . P1 prints "true" because you set component x to 100 explicitly before calling P2 . In the absence of initialization, by default or otherwise, temp.x contains any bits in memory when creating space for temp , usually by setting the stack pointer.
As an exercise, try to exclude initialization and examine the meaning:
--temp.x := 100; P2(temp); if temp.x = 100 then Ada.Text_IO.Put_Line("true"); else Ada.Text_IO.Put_Line("false"); end if; Ada.Text_IO.Put_Line(temp.x'Img & temp.y'Img);
In my implementation, the predicate fails, and temp.x contains garbage.
false 1934820168 200
Using default_expression with recording components avoids the risk of ignoring initialization.
type A is record x : integer := 0; y : integer := 0; end record;
If it depends on the compiler, then it uses in out only sure way to make sure that it works.
There is no default initialization, yes. As noted in ยง6.1 Parameter and result mechanism , "In Ada 95, it is not erroneous to depend on the parameter transfer mechanism (by reference or by copy) for those types that both allow, although this is not portable ." Since arg1 in P2 is an out parameter that can be passed in by the copy, and it is neither an access type, nor a composite type with discriminators, nor a type having an implicit initial value - ยง6.4.1 Parameter associations clearly show that "the formal parameter is not initialized " In contrast, for the parameter in out "the value of the actual parameter ... is assigned to the formal."
source share