Next: , Up: Functions   [Index]


3.4.1 Function Definition Example

A function called ‘med3’ and selecting the median value over its three real argument values may be defined as:

const struct x1f4_function_type med3 = {
    "med3",
    median3,
    X1f4_E4_REAL,
    real3,
    3,
    0,
    4
};

See struct x1f4_function_type.

The name field of the function definition specifies the name of the function: ‘med3’.

The function field selects the routine performing the function logic, a certain median3 function.

The type field specifies that the function return type is real, hence it translates X1f4_E4_REAL.

See Symbolic Types.

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

int real3[] = { X1f4_E4_REAL, X1f4_E4_REAL, X1f4_E4_REAL };

The count field specifies the number of function arguments: 3.

The flags field specifies pretty much nothing in this function definition, hence is 0.

See Function Flags.

See Operator And Function Flags.

The length field specifies the function name length, 4.

The median3 function realizes the median over a set of three, i.e. it selects the value that is neither the smallest, neither the biggest.

int
median3(void *context, void *output, void **input)
{
    X1f4_E4_C_REAL a, b, c, o, *out;

    a = *(X1f4_E4_C_REAL *) input[0];
    b = *(X1f4_E4_C_REAL *) input[1];
    c = *(X1f4_E4_C_REAL *) input[2];

    if (a < b) {
	if (b < c) {
	    o = b;
	} else {
	    if (a < c) {
		o = c;
	    } else {
		o = a;
	    }
	}
    } else {
	if (a < c) {
	    o = a;
	} else {
	    if (b < c) {
		o = c;
	    } else {
		o = b;
	    }
	}
    }

    out = output;
    *out = o;

    return 0;
}

See C Types.

The median3 function reads its logic arguments from the addresses specified by input. Conversely, the computed value is stored at the address specified by output. The return 0 value marks successful execution.


Next: Defining Function Sets, Up: Functions   [Index]