Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 12, 2006 in, out and inout for function arguments | ||||
---|---|---|---|---|
| ||||
I have a program: import std.file; public static void foo(in int i, out int o, inout int io) { i++; o--; io++; printf("foo %d, %d, %d\n", i, o, io); } int main (char[][] args) { int i1 = 1, i2 = 123, i3 = 234; printf("before foo %d, %d, %d\n", i1, i2, i3); foo(i1, i2, i3); printf("after foo %d, %d, %d\n", i1, i2, i3); return 0; } Why this compiles successfully with dmd compiler? I expect 'out' parameter modification to cause error. Is it a bug in compiler implementation or wrong usage? Also looking at sample programs I don't find in/out usage as common? |
October 12, 2006 Re: in, out and inout for function arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to SKS | On Thu, 12 Oct 2006 08:46:32 +0000 (UTC), SKS wrote: > I have a program: > import std.file; > > public static void foo(in int i, out int o, inout int io) > { > i++; o--; io++; > printf("foo %d, %d, %d\n", i, o, io); > } > > int main (char[][] args) > { > int i1 = 1, i2 = 123, i3 = 234; > printf("before foo %d, %d, %d\n", i1, i2, i3); > foo(i1, i2, i3); > printf("after foo %d, %d, %d\n", i1, i2, i3); > return 0; > } > > Why this compiles successfully with dmd compiler? > I expect 'out' parameter modification to cause error. Is it a bug in compiler > implementation or wrong usage? No it is not a bug. The 'out' qualifier causes the argument to be set to the datatype's .init value before the function gets control. The 'inout' qualifier allows the value of the argument to passed to the function by the caller, just like 'in' does. Once the function gets control, it can modify the passed arguments as much as it likes, but only changes to 'in' and 'inout' arguments get fed back to the calling code. > Also looking at sample programs I don't find in/out usage as common? That's because it is better to limit cohesion between caller and called code. The use of 'in' and 'out' reduces the possibility of side-effects that are unknown to the caller and reduces the need for the called function to be responsible for changes to data it doesn't own. When 'inout' is used, it is usually as a compromise to improve performance. For example, it can be a performance cost to copy large structs or fixed-length arrays betwen functions, and 'inout' is a way to avoid that. -- Derek Parnell Melbourne, Australia "Down with mediocrity!" |
October 12, 2006 Re: in, out and inout for function arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell |
> When 'inout' is used, it is usually as a compromise to improve
> performance.
> For example, it can be a performance cost to copy large structs or
> fixed-length arrays betwen functions, and 'inout' is a way to avoid that.
And I think that is gay. 'inout' should not be required to pass an argument
by reference.
IMO 'in' should pass by reference, but denote a read only parameter. This
would be better
for performance and correctness.
-Craig
|
October 12, 2006 Re: in, out and inout for function arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote:
> Once the function gets control, it can modify
> the passed arguments as much as it likes, but only changes to 'in' and
> 'inout' arguments get fed back to the calling code.
>
IIRC that would be: only *out* and inout arguments get fed back to the calling code.
|
October 12, 2006 Re: in, out and inout for function arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | On Thu, 12 Oct 2006 09:05:23 -0700, BCS wrote: > Derek Parnell wrote: >> Once the function gets control, it can modify >> the passed arguments as much as it likes, but only changes to 'in' and >> 'inout' arguments get fed back to the calling code. >> > > IIRC that would be: only *out* and inout arguments get fed back to the calling code. Of course ... just testing to see if you were awake ;-) -- Derek Parnell Melbourne, Australia "Down with mediocrity!" |
October 17, 2006 Re: in, out and inout for function arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Derek Parnell wrote:
>> Once the function gets control, it can modify
>> the passed arguments as much as it likes, but only changes to 'in' and
>> 'inout' arguments get fed back to the calling code.
>>
>
> IIRC that would be: only *out* and inout arguments get fed back to the calling code.
Good, I'm not crazy.
|
Copyright © 1999-2021 by the D Language Foundation