www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - in, out and inout

reply Chris Stevenson <Chris_member pathlink.com> writes:
The documantation doesn't specifically state what these mean.  I come from a
C/C++ background.

My guess is that these are special parameters for function arguments (and for
the foreach statement), and that:
in     -- agrument is read only, with modification being a compile error
out    -- argument is write only, with any attempt to evaluate being a compile
error
inout  -- argument is a reference, equivalent to C++ & before a function
argument
(none) -- argument is a value, with no interaction with calling function, and
safe to modify

Anyone wanna clairify?
--Chris Stevenson
Aug 18 2004
next sibling parent reply kinghajj <kinghajj_member pathlink.com> writes:
In article <cg0rei$20kh$1 digitaldaemon.com>, Chris Stevenson says...
The documantation doesn't specifically state what these mean.  I come from a
C/C++ background.

My guess is that these are special parameters for function arguments (and for
the foreach statement), and that:
in     -- agrument is read only, with modification being a compile error
out    -- argument is write only, with any attempt to evaluate being a compile
error
inout  -- argument is a reference, equivalent to C++ & before a function
argument
(none) -- argument is a value, with no interaction with calling function, and
safe to modify

Anyone wanna clairify?
--Chris Stevenson
No, no, no! I can only explain by example void change(in int a, out int b, inout int c) { // a is whatever was passed to the function // b is set to 0 // c is also whatever was passed to the function a = 55; b += 2; c += 3; } int main() { int a, b, c; a = 2; b = 6; c = 4; change(a,b,c); /* now, a is still 2, b is 2, and c is 7 */ } So, when a function is done, 'out' and 'inout' arguments are set to whatever the function made them, kinda like how pointers can be used in C.
Aug 18 2004
parent reply Chris Stevenson <Chris_member pathlink.com> writes:
Okay, so:
--in (the defualt if nothing is specified) keeps the vaule of the parameter for
the function, but any changes don't affect the calling function
--out resets the parameter to the default initializer, and the new vaule is
stored in the parameter from the calling function
--inout keeps the value and stores the result

Thanks,

--Chris

In article <cg0s2m$21s5$1 digitaldaemon.com>, kinghajj says...
In article <cg0rei$20kh$1 digitaldaemon.com>, Chris Stevenson says...
The documantation doesn't specifically state what these mean.  I come from a
C/C++ background.

My guess is that these are special parameters for function arguments (and for
the foreach statement), and that:
in     -- agrument is read only, with modification being a compile error
out    -- argument is write only, with any attempt to evaluate being a compile
error
inout  -- argument is a reference, equivalent to C++ & before a function
argument
(none) -- argument is a value, with no interaction with calling function, and
safe to modify

Anyone wanna clairify?
--Chris Stevenson
No, no, no! I can only explain by example void change(in int a, out int b, inout int c) { // a is whatever was passed to the function // b is set to 0 // c is also whatever was passed to the function a = 55; b += 2; c += 3; } int main() { int a, b, c; a = 2; b = 6; c = 4; change(a,b,c); /* now, a is still 2, b is 2, and c is 7 */ } So, when a function is done, 'out' and 'inout' arguments are set to whatever the function made them, kinda like how pointers can be used in C.
Aug 18 2004
next sibling parent reply Regan Heath <regan netwin.co.nz> writes:
On Thu, 19 Aug 2004 01:00:06 +0000 (UTC), Chris Stevenson 
<Chris_member pathlink.com> wrote:
 Okay, so:
 --in (the defualt if nothing is specified) keeps the vaule of the 
 parameter for
 the function, but any changes don't affect the calling function
You must however be aware exactly what the 'in' applies to, in the case of classes and built-in arrays (which are reference types) 'in' applies to the reference, not the data it refers to, so, you can change that data, even tho the class/array is passed as 'in', for example: import std.stdio; void addOne(char[] s) { s.length = s.length+1; s[s.length-1] = 'A'; } void changeOne(char[] s) { s[0] = s[0]+1; } int main() { char[] ni = "NO!"; char* p; ni.length = 100; ni.length = 3; /* add 'A' to the end of the data referenced by ni */ addOne(ni); writefln(ni); /* proof the data has changed */ p = &ni[0]; printf("%c,%c,%c,%c\n",p[0],p[1],p[2],p[3]); /* change the data within the length of the original reference */ changeOne(ni); writefln(ni); return 0; } I'm not 100% certain what the Garbage Collector makes of this, at a guess the original array and the copy both refer to the same address in memory, so even tho the copy causes that memory to be expanded the GC will not collect as the original still refers to it even tho it's length does not encompass the whole block. Of course if the expansion caused the address to move.. what then? It is documented that an append will cause a copy of the data, but my function above does not append, is it possible to make the original array invalid in this way?
 --out resets the parameter to the default initializer, and the new vaule 
 is stored in the parameter from the calling function

 --inout keeps the value and stores the result

 In article <cg0s2m$21s5$1 digitaldaemon.com>, kinghajj says...
 In article <cg0rei$20kh$1 digitaldaemon.com>, Chris Stevenson says...
 The documantation doesn't specifically state what these mean.  I come 
 from a
 C/C++ background.

 My guess is that these are special parameters for function arguments 
 (and for
 the foreach statement), and that:
 in     -- agrument is read only, with modification being a compile 
 error
 out    -- argument is write only, with any attempt to evaluate being a 
 compile
 error
 inout  -- argument is a reference, equivalent to C++ & before a 
 function
 argument
 (none) -- argument is a value, with no interaction with calling 
 function, and
 safe to modify

 Anyone wanna clairify?
 --Chris Stevenson
No, no, no! I can only explain by example void change(in int a, out int b, inout int c) { // a is whatever was passed to the function // b is set to 0 // c is also whatever was passed to the function a = 55; b += 2; c += 3; } int main() { int a, b, c; a = 2; b = 6; c = 4; change(a,b,c); /* now, a is still 2, b is 2, and c is 7 */ } So, when a function is done, 'out' and 'inout' arguments are set to whatever the function made them, kinda like how pointers can be used in C.
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 18 2004
parent reply Chris Stevenson <Chris_member pathlink.com> writes:
 --in (the defualt if nothing is specified) keeps the vaule of the 
 parameter for
 the function, but any changes don't affect the calling function
You must however be aware exactly what the 'in' applies to, in the case of classes and built-in arrays (which are reference types) 'in' applies to the reference, not the data it refers to, so, you can change that data, even tho the class/array is passed as 'in', for example:
Pretty much the same a C/C++. Is a struct past by reference or by value? (assuming no arrays/object in struct... Is an object even allowed in s struct? ) I know in C, you must pass a pointer to a struct (usually by func(&myStruct); ) in order to modify it. (In other words, do I need to declare a struct as out (or inout) to fill a struct?) --Chris
Aug 18 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Thu, 19 Aug 2004 04:28:56 +0000 (UTC), Chris Stevenson 
<Chris_member pathlink.com> wrote:
 --in (the defualt if nothing is specified) keeps the vaule of the
 parameter for
 the function, but any changes don't affect the calling function
You must however be aware exactly what the 'in' applies to, in the case of classes and built-in arrays (which are reference types) 'in' applies to the reference, not the data it refers to, so, you can change that data, even tho the class/array is passed as 'in', for example:
Pretty much the same a C/C++. Is a struct past by reference or by value?
value.
 (assuming no arrays/object in struct... Is an object even allowed in s 
 struct? )
Yep. Remember it's actually a reference to an object, not the object itself.
 I know in C, you must pass a pointer to a struct (usually by 
 func(&myStruct); )
 in order to modify it.  (In other words, do I need to declare a struct 
 as out
 (or inout) to fill a struct?)
Yes, out or inout, eg: import std.stdio; class A { } struct B { A a; } void foo(B b) { printf("foo,start : %08x\n",b.a); b.a = new A(); printf("foo,end : %08x\n",b.a); } void bar(inout B b) { printf("bar,start : %08x\n",b.a); b.a = new A(); printf("bar,end : %08x\n",b.a); } void baz(out B b) { printf("baz,start : %08x\n",b.a); b.a = new A(); printf("baz,end : %08x\n",b.a); } void main() { B b; printf("foo,before: %08x\n",b.a); foo(b); printf("foo,after : %08x\n",b.a); printf("\n"); printf("bar,before: %08x\n",b.a); bar(b); printf("bar,after : %08x\n",b.a); printf("\n"); printf("baz,before: %08x\n",b.a); baz(b); printf("baz,after : %08x\n",b.a); printf("\n"); } prints: foo,before: 00000000 foo,start : 00000000 foo,end : 00871fe0 foo,after : 00000000 bar,before: 00000000 bar,start : 00000000 bar,end : 00871fd0 bar,after : 00871fd0 baz,before: 00871fd0 baz,start : 00000000 baz,end : 00871fc0 baz,after : 00871fc0 Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 18 2004
prev sibling parent Derek Parnell <derek psych.ward> writes:
On Thu, 19 Aug 2004 01:00:06 +0000 (UTC), Chris Stevenson wrote:

 Okay, so:
 --in (the defualt if nothing is specified) keeps the vaule of the parameter for
 the function, but any changes don't affect the calling function
 --out resets the parameter to the default initializer, and the new vaule is
 stored in the parameter from the calling function
 --inout keeps the value and stores the result
 
Yes. And in other words... -- in -- (default) the value is passed to the called function. Changes made by the called program are not passed back to the calling program. NOTE though that this is sometimes confusing when passing an array. This is because the 'value' of an array is a structure { length, pointer } and not the contents of the array. Thus an 'in' array allows the called program to change the array contents but not the array 'value'. Also with class instances, the value passed is the reference to the class instance. So an 'in' class allows the called function to change the contents of a class instance but not the reference to it. If you truly do not want the called program to alter the data contained in an array or class instance, you need to pass a copy of the thing. -- out -- the value is pre-initialized to the .init property by the compiler at the time of calling. Any changes made by the called function are passed back to the caller. -- inout -- the value is passed to the called program. Any changes made by the called function are passed back to the caller. -- Derek Melbourne, Australia 19/Aug/04 11:20:58 AM
Aug 18 2004
prev sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
Chris Stevenson schrieb:

 The documantation doesn't specifically state what these mean.  I come from a
 C/C++ background.
I recall that documentation was totally clear on that matter. It just likes to be read carefully. :)
 My guess is that these are special parameters for function arguments (and for
 the foreach statement), and that:
 in     -- agrument is read only, with modification being a compile error
No, in creates a local copy. It is the same as when you don't specify anything, and it works the same way as value pass in C and C++.
 out    -- argument is write only, with any attempt to evaluate being a compile
 error
No, it is equivalent to inout for the called function, but could lead to global optimizations.
 inout  -- argument is a reference, equivalent to C++ & before a function
 argument
Yes.
 (none) -- argument is a value, with no interaction with calling function, and
 safe to modify
Yes, it is the same as "in".
 Anyone wanna clairify?
No. Ignorance provokes a wall of silence here. :> -eye
Aug 19 2004