Next: , Previous: Pass By Reference, Up: Functions


2.4.5 Pass By Reference Function Definition Example

A function called `modf' and setting its second argument to the integral part of its first argument and returning the fractional part of the same first argument, with the both output values having the same sign, just as the C modf function does may be defined as:

const struct x1f4_function_type modf_d = {
    "modf",
    modf_f,
    X1f4_E4_REAL,
    modf_l,
    2,
    X1f4_E4_POST_TYPE,
    4
};

See struct x1f4_function_type.

The args field specifies the types of the function arguments and their flags and is assumed defined as:

int modf_l[] = { X1f4_E4_REAL, X1f4_E4_REAL, 0, X1f4_E4_POST_XSET };

The flags field specifies that flags lists are defined for function arguments and hence it lists X1f4_E4_POST_TYPE.

Finally, the routine performing the function logic can be defined as:

int
modf_f(void *context, void *output, void **input)
{
    X1f4_E4_REAL f, i, x, *out;

    x = *(X1f4_E4_REAL *) input[0];

    f = modf(x, &i);

    out = output;
    *out = f;

    out = input[1];
    *out = i;

    return 0;
}