Function pointer

From Wikipedia, the free encyclopedia

A function pointer is a type of pointer in C, C++, D, and other C-like programming languages. When dereferenced, a function pointer invokes a function, passing it zero or more arguments just like a normal function. In programming languages like C, function pointers can be used to simplify code, such as replacing large switch statements.

Function objects, or functors, are similar to function pointers, and can be used in similar ways. A functor is a object of a class type that implements the function-call operator, allowing the object to be used within expressions using the same syntax as a function call. Functors are more powerful than simple function pointers, being able to contain their own data values, and allowing the programmer to emulate closures, among other uses.

Many "pure" object-oriented languages (such as Java) do not support function pointers. Something similar can be implemented in these kinds of languages, though, using references to interfaces that define a single member function.

In other languages that support first-class functions, functions are regarded as data, and can be passed, returned, and created dynamically directly by other functions, eliminating the need for function pointers.

[edit] Examples in C

This example demonstrates the use of function pointers. Here a function pointer new_function is declared, the address of the function named my_function() is assigned to it, and the function is then invoked by dereferencing the pointer.

#include <stdio.h>

static int my_function(int a)
{
    printf("my_function: %d\n", a);
    return (2*a + 3);
}

int main(void)
{
    int (*new_function)(int a) = my_function;
    int  x;

    x = (*new_function)(10);
    printf("main: %d\n", x);
    return 0;
}

Note that the syntax (*fp)(arg) is used to invoke the function indirectly through function pointer fp. C syntax also allows the simpler fp(arg) to be used, since the fp subexpression evaluates to the address of a function whether it is a function name or a pointer to a function.

The next example shows how function pointers can be used as parameters to other functions. As in the previous example, a function named function() is defined which will be called through a function pointer. A function named caller() accepts as parameters a function pointer and an integer, which will be passed as arguments to the function that will be called by caller(). The caller's first parameter will accept the name (address of) any function that matches the prototype of the function pointer.

#include <stdio.h>

static void function(int a)
{
    printf("my_function: %d\n", a);
}

static void caller(void (*new_function)(int a), int p)
{
    new_function(p);
}

int main(void)
{
    caller(function, 10);
    return 0;
}

The third example also shows how function pointers can be used as parameters to other functions. Here a function f() is passed to function integ() which approximates an integral evaluated over an interval, \textstyle\int_{a}^{b} f(x)\;dx, calling the function f(x) for each value of x. Any function taking a single double argument and returning a double result can be passed to integ() for evaluation.

double integ(double a, double b, double (*f)(double x))
{
    double  sum = 0.0;
    double  x;
    int     n;

    // Evaluate integral{a,b} f(x) dx
    for (n = 0;  n <= 100;  n++)
    {
        x = (n/100.0)*(b-a) + a;
        sum += (*f)(x) * (b-a)/101.0;
    }
    return sum;
}

[edit] External links