I'm curious about the initialization within Ada procedures: Suppose I have the following procedure:
procedure Foo (Bar : Integer) is
Another_Bar : Integer := Bar;
begin
...
end Foo;
Should the assignment to Another_Bar have the same overhead as
procedure Foo2 (Bar : Integer) is
Another_Bar : Integer;
begin
Another_Bar := Bar;
...
end Foo;
My question is essentially if both assignments generate the same assembly instructions and thus are equal in speed? (without detailing the target machine)
Based on the Ada language standard, there is no general reason why those two forms of code should have different performance. It would all depend on the target machine and the compiler being used. Depending on the rest of the code in the procedure, some compilers could even completely optimize away the
Another_Barvariable.However, there is a semantic difference, which could be important if the subtypes of
BarandAnother_Barwere different -- for example, ifAnother_Barwere declared asPositiveinstead ofInteger. Namely, in the first form any exception raised by the initialization ofAnother_Bar(say, becauseBarhas a negative value) is not handled by the possible exception handlers in the procedure itself, but is propagated to the caller. In the second form, whereAnother_Baris assigned after thebegin, exceptions from that assignment can be handled by the procedure's own exception handlers.