Definition

Function pointers point to executable code at a particular piece of memory. Dereferencing the function pointer allows the code in the memory block to be executed. Main benefit of function pointers is that they provide a straightforward mechanism for choosing a function to execute at runtime.

Important points about Function Pointer

  • Unlike normal pointers, a function pointer points to code, not data. A function pointer stores the start address of executable code.
  • Function name can also be used to get functions address. Also address operator (&) can also be used.
  • A function pointer can be passed as an argument and can also be returned from a function.

Syntax

Following illustrates the syntax of declaring a function pointer:

<returnType> (*<functionName>) (functionArguments);

Syntax of declaring a function pointer is similar to the syntax of declaring a function. Only difference is that instead of using the function name, use a pointer name inside the parentheses (). In the above syntax

  • returnType is the return type of the function pointer. It can be any valid type such as int, float, char, or void.
  • Name of function pointer is inside parentheses.
  • functionArguments specify all parameters of the function with their corresponding types. Function pointer only can refer to a function with the same signature.
void (*pf)(int);

In above example *pf is the pointer to a function. void is the return type of that function, and int is the argument type of that function.

Defining Function Type

It is convenient to use a typedef to define the function type. For example

typedef void (*functionPtr)(int);

Above defined a type functionPtr. Variables of this type point to function that take an int as a parameter and don’t return anything. It can be use to declare variable of type functionPtr as shown below

functionPtr fPtr

Example

Below example shows usage of function pointer. Function op takes three argument, last argument is function pointer.

double add(double val1, double val2) {
  return val1 + val2;
}

double substract(double val1, double val2) {
  return val1 * val2;
}

double op(double val1, double val2, double (*functionPtr)(double, double)) {
  return (*f)(val1, val2);
}

int main() {

  double a = 5.0;
  double b = 10.0;

  std::cout << "Add: " << op(a, b, add) << std::endl;
  std::cout << "Substract: " << op(a, b, substract) << std::endl;

  return 0;
}

functionPtr specify returns double and takes two double values. op dereferences the function pointer in order to execute the correct function – add or substract.

Below shows usage of function pointer.

// Define a function pointer and initialize to NULL
// pt2Function is a pointer to a function which returns an int and takes a float and two char
int (*pt2Function)(float, char, char) = NULL;

int DoMore(float a, char b, char c) {
  return a - b + c;
}

// Assign an address to a Function Pointer
pt2Function = &DoMore;

// Calling a Function using a Function Pointer
int result1 = pt2Function(12, 'a', 'b');
int result2 = (*pt2Function) (12, 'a', 'b');

// Pass a Function Pointer as an Argument.
void PassPtr(int (*pt2Function)(float, char, char))
{
  // call using function pointer
  int result = (*pt2Function)(12, 'a', 'b');
}

// Declare function pointer type
typedef float(*pt2Function)(float, float);

float Plus(float a, float b) {
  return a + b;
}

float Minus(float a, float b) {
  return a - b;
}

// Function takes a char and returns a function pointer which is defined with the typedef above.
pt2Function GetPtr2(const char opCode)
{
  if (opCode == '+') {
    return &Plus;
  }
  else {
    return &Minus;
  }
}

// Arrays of Function Pointers
typedef int (*pt2Function)(float, char, char);

void Array_Of_Function_Pointers()
{
  // Define arrays and set each element to NULL
  pt2Function funcArr[10] = { NULL };

  // Assign the function's address
  funcArr[0] = funcArr[1] = &DoMore;
}

Reference

Function pointer