A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments just as in a normal function call. Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed identifier or address.

Function pointers allow different code to be executed at runtime. They can also be passed to a function to enable callbacks.

Function pointer

Function pointers are supported by third-generation programming languages (such as PL/I, COBOL, Fortran, dBASE dBL, and C) and object-oriented programming languages (such as C++, C#, and D).

Function pointer

Simple function pointers

The simplest implementation of a function (or subroutine) pointer is as a variable containing the address of the function within executable memory. Older third-generation languages such as PL/I and COBOL, as well as more modern languages such as Pascal and C generally implement function pointers in this manner.

Function pointer

Example in C

The following C program illustrates the use of two function pointers: func1 takes one double-precision (double) parameter and returns another double, and is assigned to a function which converts centimeters to inches. func2 takes a pointer to a constant character array as well as an integer and returns a pointer to a character, and is assigned to a C string handling function which returns a pointer to the first occurrence of a given character in a character array.

#include <stdio.h> /* for printf */

#include <string.h> /* for strchr */

double cm_to_inches(double cm) {

return cm / 2.54; }

// "strchr" is part of the C string handling (i.e., no need for declaration) // See https://en.wikipedia.org/wiki/C_string_handling#Functions

int main(void) {

double (*func1)(double) = cm_to_inches; char * (*func2)(const char *, int) = strchr; printf("%f %s", func1(15.0), func2("Wikipedia", 'p')); /* prints "5.905512 pedia" */ return 0; }

The next program uses a function pointer to invoke one of two functions (sin or cos) indirectly from another function (compute_sum, computing an approximation of the function's Riemann integration). The program operates by having function main call function compute_sum twice, passing it a pointer to the library function sin the first time, and a pointer to function cos the second time. Function compute_sum in turn invokes one of the two functions indirectly by dereferencing its function pointer argument funcp multiple times, adding together the values that the invoked function returns and returning the resulting sum. The two sums are written to the standard output by main.

#include <math.h>

#include <stdio.h>

// Function taking a function pointer as an argument

double compute_sum(double (*funcp)(double), double lo, double hi) {

double sum = 0.0;

// Add values returned by the pointed-to function '*funcp'

int i; for (i = 0; i <= 100; i++) { // Use the function pointer 'funcp' to invoke the function double x = i / 100.0 * (hi - lo) + lo; double y = funcp(x); sum += y; } return sum / 101.0 * (hi - lo); }

double square(double x) {

return x * x; }

int main(void) {

double sum;

// Use standard library function 'sin()' as the pointed-to function

sum = compute_sum(sin, 0.0, 1.0); printf("sum(sin): %g\n", sum);

// Use standard library function 'cos()' as the pointed-to function

sum = compute_sum(cos, 0.0, 1.0); printf("sum(cos): %g\n", sum);

// Use user-defined function 'square()' as the pointed-to function

sum = compute_sum(square, 0.0, 1.0); printf("sum(square): %g\n", sum);

return 0; }

Functors

Functors, or function objects, 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. They are also used as callback functions if it is necessary to use a member function as a callback function.