Next: , Previous: , Up: Language Miscellanea   [Index]


13.3 Alternative Non Scalar Function Returns

aime does allow non scalar returns.

Sample function setting up a ‘list’ object and returning it:

list
f0(void)
{
    integer i;
    list l;

    i = 0;
    while (i < 64) {
	lb_p_integer(l, i);
	i += 1;
    }

    return l;
}

See The List Library.

Alternatively, where proper non scalar returns are not possible or desirable, non scalar values may still be extracted out of functions.

aime provides for pass by reference. Getting non scalars out of functions may be achieved via this mechanism, by passing a variable as function argument where a reference parameter is expected and set the parameter from within the function logic.

Sample function setting its reference string parameter:

void
f1(text &s)
{
    s = "string of characters";
}

Call it:

    text x;

    f1(x);
    # _x_ is now `string of characters'

Sample function setting its reference sequence parameter:

void
f2(list &l)
{
    integer i;
    list a;

    i = 0;
    while (i < 64) {
	# append _i_ to sequence
	lb_p_integer(a, i);
	i += 1;
    }

    l = a;
}

Call it:

    list v;

    f2(v);
    # _v_ is now 64 long

It is worth noting that aime objects have an inherent reference nature. Except for those of intrinsic types, aime objects are references themselves. So the ‘list’ objects in the above example are references of some deeper objects implementing them, and as such, passing aime objects as function (non reference) parameters do not copy the actual proper objects, but merely the references to them. It also means that objects outside function scope may be modified through non reference parameters.

Sample function modifying its sequence parameter:

void
f3(list a)
{
    integer i;

    i = 0;
    while (i < 64) {
	lb_p_integer(a, i);
	i += 1;
    }
}

Call it:

    list v;

    f3(v);
    # _v_ is now 64 long

Copying the datum to return into one of the parameters is another way to accomplish the task. Same function, modified to copy a local variable into the parameter:

void
f4(list a)
{
    integer i;
    list l;

    i = 0;
    while (i < 64) {
	lb_p_integer(l, i);
	i += 1;
    }

    l_copy(a, l);
}

Next: , Previous: , Up: Language Miscellanea   [Index]