Thread overview
What's the difference between "out" and "inout"?
May 21, 2006
Hasan Aljudy
May 21, 2006
Hasan Aljudy
May 21, 2006
Lionello Lunesu
May 21, 2006
Deewiant
May 21, 2006
Maybe a silly question, but what's the difference bebtween "out" parameters and "inout" parameters?
For a long time I was under the impression that there's no difference ..  but I'm not sure anymore.
May 21, 2006
There's a big difference:

out initializes the variable to the default initializer.
inout does not change the value passed in.

Example:

import std.stdio;

int foo1(out i)
{
   writefln(i);
}

int foo2(inout i)
{
   writefln(i);
}

int main()
{
   int i;

   i = 5;
   foo1(i);

   i = 5;
   foo2(i);

   return 0;
}

Will output:

0
5

Because in the first case i is set to 0 because it is an int.  Out means that the value before the call doesn't matter; with inout it may matter.

-[Unknown]


> Maybe a silly question, but what's the difference bebtween "out" parameters and "inout" parameters?
> For a long time I was under the impression that there's no difference ..  but I'm not sure anymore.
May 21, 2006
Interesting, but why? What situations need this kind of behaviour?

Unknown W. Brackets wrote:
> There's a big difference:
> 
> out initializes the variable to the default initializer.
> inout does not change the value passed in.
> 
> Example:
> 
> import std.stdio;
> 
> int foo1(out i)
> {
>    writefln(i);
> }
> 
> int foo2(inout i)
> {
>    writefln(i);
> }
> 
> int main()
> {
>    int i;
> 
>    i = 5;
>    foo1(i);
> 
>    i = 5;
>    foo2(i);
> 
>    return 0;
> }
> 
> Will output:
> 
> 0
> 5
> 
> Because in the first case i is set to 0 because it is an int.  Out means that the value before the call doesn't matter; with inout it may matter.
> 
> -[Unknown]
> 
> 
>> Maybe a silly question, but what's the difference bebtween "out" parameters and "inout" parameters?
>> For a long time I was under the impression that there's no difference ..  but I'm not sure anymore.
May 21, 2006
I think you should look at "out" as if it were the function's return value:

#void func(out int i) { return i; }
#int func() { int i; return i; }

I think these two should behave the same way. In fact, they do :)

L.

"Hasan Aljudy" <hasan.aljudy@gmail.com> wrote in message news:e4p6vo$1kmn$1@digitaldaemon.com...
> Interesting, but why? What situations need this kind of behaviour?
>
> Unknown W. Brackets wrote:
>> There's a big difference:
>>
>> out initializes the variable to the default initializer. inout does not change the value passed in.
>>
>> Example:
>>
>> import std.stdio;
>>
>> int foo1(out i)
>> {
>>    writefln(i);
>> }
>>
>> int foo2(inout i)
>> {
>>    writefln(i);
>> }
>>
>> int main()
>> {
>>    int i;
>>
>>    i = 5;
>>    foo1(i);
>>
>>    i = 5;
>>    foo2(i);
>>
>>    return 0;
>> }
>>
>> Will output:
>>
>> 0
>> 5
>>
>> Because in the first case i is set to 0 because it is an int.  Out means that the value before the call doesn't matter; with inout it may matter.
>>
>> -[Unknown]
>>
>>
>>> Maybe a silly question, but what's the difference bebtween "out"
>>> parameters and "inout" parameters?
>>> For a long time I was under the impression that there's no difference ..
>>> but I'm not sure anymore.


May 21, 2006
Hasan Aljudy wrote:
> Interesting, but why? What situations need this kind of behaviour?
> 

In current D it's rarely actually needed, since objects' and arrays' contents can be modified regardless of in, out, or inout, and it's usually the contents and not the reference that is changed. But basically the kind of situation would be something like the following:

void empty(out Array x) { x.length = 0; }

I hope you get the idea. It does come up quite rarely.
May 21, 2006
Image a state-based function that did not utilize a class for storing its state information.

bool do_something(in Stream s, inout int state);

Then you might do this:

int state = 0;
while (do_something(s, state))
   writefln("Current state: ", state, "\n");

This isn't the best example, I'm afraid, but it's the clearest one I can think of at the moment.  It's not needed every day, but it has definite uses.

I guess another option would be:

bool do_something(Stream s, in int previous_state, out new_state);

But that seems silly.

-[Unknown]


> Interesting, but why? What situations need this kind of behaviour?