Thread overview
out / in / inout
May 18, 2004
manfred
May 18, 2004
Mike Swieton
May 18, 2004
Hauke Duden
May 18, 2004
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
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
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