I have a quick Ada question. If I have a procedure where I may write out to a variable, or I might leave it alone, should it be an Out parameter or an In Out parameter? I guess this boils down to the question:
What does the caller see if it calls a procedure with a parameter as Out but the procedure doesn’t touch the parameter. Does it see the same value? Undefined behavior?
The compiler doesn’t complain because it sees an assignment to the Out variable…it just happens to be in a conditional, where it may not be reached, and the compiler doesn’t bother to check all paths.
I suspect the safe bet is marking the parameter as In Out, but I’d like to know if this is necessary or just stylistically preferable.
Thanks!
-prelic
In Ada, when a procedure with an
outparameter does not write anything to that parameter, the result passed back to the caller is something undefined. This means that whatever was in that variable in the caller, gets overwritten by garbage on return from the procedure.The best practice in Ada is to definitively initialise all
outparameters with a suitable default value at the start of the procedure. That way, any code path out of the procedure results in valid data passed back to the caller.If you have something in the caller that might be changed by a procedure, you must use an
in outparameter.From the Ada 95 RM 6.4.1 (15):