Next: , Previous: , Up: Top   [Index]

9 Function Pointers

Function pointers are values indicating functions.

Function pointer variables are declared much like functions are, with the variable name preceded in order by a ‘(’ sign and a ‘*’ sign and followed by a ‘)’ sign.

    integer (*fp1)(integer);
    real (*fp2)(real, real);
    text (*fp3)(text, integer);
    object (*fp4)(object, &...);

Multiple declarations may occur on the same declarations line and may be mixed with declarations of variables of the return type and declarations of function pointer variables of the same return type.

    integer (*fp5)(integer), a, b, (*fp6)(integer, integer);
    real c, (*fp7)(real, real);
    void (*fp8)(integer), (*fp9)(text, ...);

The proper aime type of function pointers reflects the fully qualified signature of the assignable functions, return type and arguments list included. Thus ‘fp1’ and ‘fp5’ have the same type (‘integer (*)(integer)’), but the ‘fp5’ and ‘fp6’ do not (the type of ‘fp6’ is ‘integer (*)(integer, integer)’).

Function pointer variables may be assigned from functions and function pointers, via the ‘=’ function pointer assignment operator, the ‘set’ function argument assignment function, etc.

See Positional Arguments Access.

    fp1 = abs;
    fp5 = fp1;
    fp6 = min;
    fp2 = pow;
    fp4 = call;
pointers_1(integer (*&f)(integer, integer))
    f = max;
pointers_2(integer (*&f)(integer, integer))
    set(0, max);

Functions may be viewed as read-only function pointer variables, though the view may not always apply. The aime interpreter will make function pointers out of functions where required.

The functions indicated by function pointers are called via the function pointers the way they would be called by their name, with the function pointer substituting the function name.

    fp2(.5, 2);
    fp3("aime", 6);
    fp4(pow, 4, 4);
    call(fp5, 4);
    call(fp6, 4, 8z);
    trap(fp7, 0, .25);

Like any value, function pointers may be passed to functions as arguments or returned by functions.

pointers_3(integer (*f)(integer), integer (*g)(integer), integer (*h)(integer))
    return call($drand(2), -88);

The function ‘pointers_3’ randomly calls a function in its input set and returns the result.

integer (*pointers_4(void))(integer)
    return abs;

The function ‘pointers_4’ returns a function pointer indicating the ‘abs’ function.

integer (*pointers_5(integer (*f)(integer), integer (*g)(integer),
		     integer (*h)(integer)))(integer)
    return $drand(2);

The function ‘pointers_5’ randomly returns one of its arguments.

Function calls through unassigned function pointers will fail.

Next: , Previous: , Up: Top   [Index]