Thread overview
in/out parameters in function
Jul 26, 2004
jaraslov pavlock
Jul 26, 2004
Sha Chancellor
Jul 27, 2004
Regan Heath
Jul 26, 2004
teqDruid
Jul 26, 2004
Andrew Edwards
Jul 27, 2004
Derek Parnell
July 26, 2004
Hi all.
I read d spec, and i see in and out parameters for functions.
Can someone explain?




July 26, 2004
In article <ce3lvb$1djn$1@digitaldaemon.com>,
 "jaraslov pavlock" <bocj@suse.cz> wrote:

> Hi all.
> I read d spec, and i see in and out parameters for functions.
> Can someone explain?

According to walter this morning, He scrapped that feature along time ago.  I liked the idea personally, it was going to be something like this:



#int main(char[][] args) {
#
#  foreach( char [] arg; args )
#  in
#   {
#      assert( arg.length > 10 )
#   }
#   {
#      printf( "..." );
#   }
#}



Kind of like that.
July 26, 2004
On Mon, 26 Jul 2004 15:27:26 -0400, jaraslov pavlock wrote:

> Hi all.
> I read d spec, and i see in and out parameters for functions. Can someone
> explain?

something like this:

void foo(in int x, out int y, inout int z)
{
	y = z;
	z = x;
}

the in/out/inout specify whether the parameter is for input, output, or both.  In this example, the variable x is for input- it shouldn't be changed in the function, and after the function will remain the same. Out- the variable is used for output from the function, inout- both are possible.

Running this:
int x,y,z;
x = 5;
z = 7;
foo(x,y,z);
writefln("x: %d, y: %d, z: %d", x, y, z);

should produce this output:
x: 5, y: 7, z: 5

Have I got it right?
July 26, 2004
teqDruid wrote:
> On Mon, 26 Jul 2004 15:27:26 -0400, jaraslov pavlock wrote:
> 
> 
>>Hi all.
>>I read d spec, and i see in and out parameters for functions. Can someone
>>explain?
> 
> 
> something like this:
> 
> void foo(in int x, out int y, inout int z)
> {
> 	y = z;
> 	z = x;
> }
> 
> the in/out/inout specify whether the parameter is for input, output, or
> both.  In this example, the variable x is for input- it shouldn't be
> changed in the function, and after the function will remain the same. Out-
> the variable is used for output from the function, inout- both are
> possible.
> 
> Running this:
> int x,y,z;
> x = 5;
> z = 7;
> foo(x,y,z);
> writefln("x: %d, y: %d, z: %d", x, y, z);
> 
> should produce this output:
> x: 5, y: 7, z: 5
> 
> Have I got it right?

Yes you have!
July 27, 2004
On Mon, 26 Jul 2004 15:27:26 -0400, jaraslov pavlock wrote:

> Hi all.
> I read d spec, and i see in and out parameters for functions.
> Can someone explain?

Here's my understand, which may need tweaking ;-)

'in' (the default) means that data is passed to your function, and you cannot change that data. However note that if you pass a *class* member, the data actually being passed is the reference to that member. This means that you can't change the reference but you can still change the information inside that member. This is important to know if you pass a string (char[]) and expect that the routine cannot change it, because it can. In this case, it might be better to pass a copy of the string.

'out' means that no data is passed from the calling routine. The parameter initially contains the .init value. You can modify it and the modifications will be passed back to the caller.

'inout' is a combination of these. That is, data is passed to your routine from the calling routine and you can modify it such that the modifications are passed back to the caller.

<code example>
import std.stdio;
class bar
{
    long X;
    this() { X = 100; }
}

struct bridge
{
    long X;
}

void foo(in long A, out long B, inout long C)
{
    writef("foo basic  %4d %4d %4d\n", A,B,C );
    B = A + C;
    A = -A;
    C = -C;
}

void foo(in bar A, out long B, inout long C)
{
    writef("foo class  %4d %4d %4d\n", A.X,B,C );
    B = A.X + C;
    A.X = -A.X;
    C = -C;
}

void foo(in bridge A, out long B, inout long C)
{
    writef("foo struct %4d %4d %4d\n", A.X,B,C );
    B = A.X + C;
    A.X = -A.X;
    C = -C;
}

void modstr(in char[] A)
{
    writef("modstr in  '%s'\n", A );
    A[0] = '*';
}

static char[] astr = "abc";
static char[] bstr = "def";

void modstr(in char[] A, inout char[] B)
{
    writef("modstr in  '%s', '%s'\n", A, B );
    A = astr;
    B = bstr;
}

void main()
{
    long a,b,c;
    bar x = new bar;
    bridge y;
    char[] strone;
    char[] strtwo;

    a = 1;
    b = 2;
    c = 3;
    foo(a,b,c);
    writef("result 1   %4d %4d %4d\n", a,b,c );

    x.X = 1;
    b = 2;
    c = 3;
    foo(x,b,c);
    writef("result 2   %4d %4d %4d\n", x.X,b,c );

    y.X = 1;
    b = 2;
    c = 3;
    foo(y,b,c);
    writef("result 3   %4d %4d %4d\n", y.X,b,c );

    strone = "hello world";
    modstr(strone);
    writef("result 4   '%s'\n", strone );

    strone = "hello";
    strtwo = "world";
    modstr(strone, strtwo);
    writef("result 5   '%s', '%s'\n", strone, strtwo );


}

</code>

Hope this helps.
-- 
Derek
Melbourne, Australia
27/Jul/04 10:10:54 AM
July 27, 2004
On Mon, 26 Jul 2004 13:26:18 -0700, Sha Chancellor <schancel@pacific.net> wrote:

> In article <ce3lvb$1djn$1@digitaldaemon.com>,
>  "jaraslov pavlock" <bocj@suse.cz> wrote:
>
>> Hi all.
>> I read d spec, and i see in and out parameters for functions.
>> Can someone explain?
>
> According to walter this morning, He scrapped that feature along time
> ago.  I liked the idea personally, it was going to be something like
> this:
>
>
>
> #int main(char[][] args) {
> #
> #  foreach( char [] arg; args )
> #  in
> #   {
> #      assert( arg.length > 10 )
> #   }
> #   {
> #      printf( "..." );
> #   }
> #}
>
>
>
> Kind of like that.

Depends whether the OP meant in/out/inout function parameter modifiers (see teqDruids post) or in/out DBC blocks.

The OP did say "functions" not "loops", so while you're correct in/out DBC blocks do not work in loops (Walter did mention he needs to remove that from the spec) they still work for functions. eg.

int foo(int bar)
in {
  assert(bar > 0);  //bar must be greater than 0
}
out (result) {
  assert(result == bar+5); //check the returned result is bar+5
}
body {
  return bar + 5;  //actual function body goes here
}

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/