Function Pointer Article Index for
Function
Website Links For
Function
 

Information About

Function Pointer




Function Objects , or '''functors''', are similar to function pointers, and can be used in similar ways. A functor is an 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 . Microsoft .NET languages such as C# and Visual Basic .NET implement Type-safe function pointers with Delegates .

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


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

static int my_function(int a)
{
printf("my_function: %d
", a);
  • a + 3);

  • }


int main(void)
{
  • new_function)(int) = my_function;

  • int x;


  • new_function)(10);

  • printf("main: %d

", x);
return 0;
}

  • 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

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

  • new_function)(int), 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, extstyle\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.

  • f)(double))

  • {

double sum = 0.0;
double x;
int n;

// Evaluate integral{a,b} f(x) dx
for (n = 0; n <= 100; n++)
{
  • (b-a) + a;

  • f)(x) --- (b-a)/101.0;

  • }

return sum;
}

For defining an array of function pointers, it might be useful to use a Typedef to declare a new type for your function pointer.

#include

  • '''Fx''')(double);


int main(void)
{
Fx f {Link without Title} ;

f {Link without Title} = cos;
f {Link without Title} = sin;
f {Link without Title} = tan;

printf("From 0 to pi/4:
");

printf(" integ of cos = %g
", integ(0, M_PI/4, f {Link without Title} ));
printf(" integ of sin = %g
", integ(0, M_PI/4, f {Link without Title} ));
printf(" integ of tan = %g
", integ(0, M_PI/4, f {Link without Title} ));

return 0;
}


EXTERNAL LINKS