www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - out / in / inout

reply manfred toppoint.de writes:
Hello,  
here is the test programm:  
  
int global_var;
void fn(inout int a, out int b, in int c) {  
        a = 3;  
        printf("a
= %d\n", a);  
        printf("b = %d\n", b);  
        printf("c = %d\n", c);
printf("global_var = %d\n", global_var);  
}
int main()  
{  
        global_var = 2;  
        fn(global_var,
global_var,global_var);  
        return 0;  
}  
  
The programm prints out:
a = 3  
b = 3  
c = 2  
global_var = 3  
  
  
Why is b = 3.   
I can't figure
out the difference between out and inout.  
Is "inout" call by reference ?  
If
i change "out" to "in"  then  b=2.   
  
"in" is call by value, is this right?
Manfred  
  
May 18 2004
next sibling parent Mike Swieton <mike swieton.net> writes:
On Tue, 18 May 2004 14:28:03 +0000, manfred wrote:
 Why is b = 3.   
 I can't figure
 out the difference between out and inout.  
 Is "inout" call by reference ?  
 If
 i change "out" to "in"  then  b=2.   
   
 "in" is call by value, is this right?
 Manfred
'In' copies the value in (If your talking about objects, keep in mind that the value you pass around is a memory location, not the object). 'Inout' will allow assignment, i.e. pass by reference. Out will initialize the parameter to the default value before the function is entered. b == 3 because you assign a = 3, and a is passed by reference. A and B both reference the same variables, because they are passed by out/inout. Mike Swieton __ We owe most of what we know to about one hundred men. We owe most of what we have suffered to another hundred or so. - R. W. Dickson
May 18 2004
prev sibling parent Hauke Duden <H.NS.Duden gmx.net> writes:
manfred toppoint.de wrote:
 Hello,  
 here is the test programm:  
   
 int global_var;
 void fn(inout int a, out int b, in int c) {  
         a = 3;  
         printf("a
 = %d\n", a);  
         printf("b = %d\n", b);  
         printf("c = %d\n", c);
 printf("global_var = %d\n", global_var);  
 }
 int main()  
 {  
         global_var = 2;  
         fn(global_var,
 global_var,global_var);  
         return 0;  
 }  
   
 The programm prints out:
 a = 3  
 b = 3  
 c = 2  
 global_var = 3  
   
   
 Why is b = 3. 
Because a and b are both references to global_var. When you do a=3 you change global_var and since b also refers to it b is 3.
 I can't figure
 out the difference between out and inout.  
 Is "inout" call by reference ?  
 If
 i change "out" to "in"  then  b=2.   
Both out and inout are call-by-reference. The only difference is that "out" parameters are automatically initialized to the type's default initializer at the start of the function. For example, if you have a parameter "out int a" then "a" would be automatically initialized to 0.
 "in" is call by value, is this right?
Yes, except for class objects, since they are always passed by reference. Note that "in" is the default, i.e. it is what you get when you specify neither "inout", "out" or "in". Hauke
May 18 2004