What's the difference between parameters declared with var
and those declared with out
? How does the compiler treat them differently (e.g., by generating different code, or by changing which diagnostics it issues)? Or do the different modifiers merely allow the programmer to document intended use of the parameters? What effect do the types of the parameters have on the matter?
-
8I have always had the same question, never bothered to ask though.– Jerry DodgeJan 24, 2013 at 17:39
-
Me too. I've actually run into this problem, so I would like a really good answer to this one.– Glenn1234Jan 24, 2013 at 17:48
-
1Documentation makes it pretty clear, IMO: docwiki.embarcadero.com/RADStudio/XE3/en/…– ainJan 24, 2013 at 19:29
-
3@ain It is clear. It's just incorrect. It's only accurate for managed types.– David HeffernanJan 24, 2013 at 19:41
-
maybe asking Embarcadero will make the difference...– RBAJan 24, 2013 at 21:10
4 Answers
A var
parameter will be passed by reference, and that's it.
An out
parameter is also passed by reference, but it's assumed that the input value is irrelevant. For managed types, (strings, Interfaces, etc,) the compiler will enforce this, by clearing the variable before the routine begins, equivalent to writing param := nil
. For unmanaged types, the compiler implements out
identically to var
.
Note that the clearing of a managed parameter is performed at the call-site and so the code generated for the function does not vary with out
or var
parameters.
-
This answer was nice and simple, but now I've read Serg's (deleted) answer, and I'm confused again. He says
out
is important for interface parameters of external functions because other (non-Delphi) code might call it with a "rubbish value." But if the only difference betweenvar
andout
is in the way the compiler handles the calling side, and the calling side isn't in Delphi in Serg's scenario, then I don't understand what distinction Serg is trying to point out. Jan 24, 2013 at 21:03 -
2@Rob I think Serg's a bit confused here. He get's it right on his blog: sergworks.wordpress.com/2012/10/01/… Jan 24, 2013 at 21:29
-
The more I think why
out
params were implemented so the more I wonder. There is no need to nil theout
parameter outside of the procedure in native Delphi code, and I could not find out how the current implementation ofout
parameters can help to make a bridge to other languages that treat interface-typeout
params differently.– kludgJan 25, 2013 at 7:30 -
2@Serg, if other languages don't clear the output parameter before assigning to it, and Delphi doesn't clear it before the call, then that's a memory leak. Delphi doesn't know what the called function will do, so the only safe choice is to always clear the variable before passing it to the other code. Better to clear the variable twice than not at all. Jan 25, 2013 at 14:41
-
1@RobKennedy +1 possible memory leak on the Delphi side is a sufficient explanation, thank you.– kludgJan 25, 2013 at 18:51
There is not much difference, for the compiler that is. See Mason's answer for that.
Semantically, there is a big difference:
var
tells the programmer that the routine could work with its current value,out
tells the programmer that the routine will ignore/discard its current value.
-
8@David, this answer is dealing with documentation, not actual behavior. I think most people generally assume that
out
in Delphi works the same way asout
in C#, where it's illegal to read the parameter before it's been written. When people declareout
parameters in Delphi, I think that's usually how they intend to treat them. Usingout
instead ofvar
serves to document that intention, even if the compiler doesn't actually enforce it. Reading anout
parameter before writing to it is a mistake and a bug, even if the compiler doesn't flag it as an error. Jan 25, 2013 at 15:07 -
1@Rob If the compiler doesn't check, then the whole edifice collapses. You the programmer have to go and check the function before you call it. I mean, when writing a function, I use
out
to document my intent. But when I come across a function with an out parameter, I have to go and check. Jan 25, 2013 at 15:13 -
2@NGLN I totally get it. I fully understand the semantics of out. It's just that since the compiler doesn't enforce it, the function may very well use the value I feed in. In other words the author of the routine can lie to me, and the compiler doesn't complain. Imagine if the compiler didn't bother enforcing type safety. Just because the function said it expects to receive a parameter of one type, the errant programmer could send something else. Jan 25, 2013 at 16:33
-
1The problem IMO is that the documentation concerning how Delphi treats
out
parameters was probably written for Delphi.NET or else, but not for Delphi. Delphi compiler just cannot implementout
parameters according to the documentation, and it raises questions whatout
parameters really are and when they should be used.– kludgJan 25, 2013 at 19:02 -
2I think that if a programmer uses "out" then he is declaring that he will not read the value and assume it is useful. I personally only check if someone has written a routine properly if I have problems. So I think NGLN's answer is spot on.– DsmSep 16, 2015 at 15:09
Slightly late but just for the record, I came across a case where var or out made a big difference.
I was working on a SOAP web service which exported the following method:
function GetUser( out User :TUser ) :TResult;
which was getting imported into C# as the equivalent of
function GetUser( out Result :TResult) :TUser;
when I changed the out to a var it it imported correctly.
I'm guessing that the Delphi SOAP invoker treats the function result as an out parameter and that having two out parameters confuses the Delphi SOAP routines. I'm not sure if there is a workaround to allow you to use out parameters.
I read earlier that out parameter is set to default by called function, but today I realized that it is not completely true. Value of out parameter is discarded by called routine, but if that routine does not change its value, caller can still get it initial value, which was assigned before passing to called thread.
For example:
procedure JustNothing(out x : integer);
begin
// do nothing
end;
procedure TestOutVar;
var i : Integer;
begin
i := 100;
JustNothing(i); // after this call, i will still be 100
end;
-
1AFAIK, it's only for managed types (ie. interfaces and strings, and probably records that contain these, and - according to earlier comment - also CLASS variables) that the caller clears OUT parameters before calling the function. "Integer" is not such a type, so an OUT parameter is the same as a VAR parameter in this case (except that it documents the intent on not to rely on any initial value in the function). Sep 1, 2022 at 7:16
-
Did you intend for this to be an answer to my question? It seems like you're just sharing one limited observation, without really attempting to form it into a rule or other explanation for why you've observed what you did. Jan 30 at 16:36