How to use Function Pointers in C++


Note: This article is intended for intermediate C++ programmers. If you don't know what a pointer is, this article will only severely confuse you, most likely to the point of paralysis. You have been warned.

In C++ you can, quite literally point to anything. Most often we use pointers to large data structures, or self-referencing pointers in the cases of many abstract data types (such as linked lists, trees, etc.). We've all used pointers to character arrays (the good ol' char *'s), but one neat pointer type many might not know about is pointers to functions.

Since a function is nothing more than a chunk of code, it makes sense that we can point to that section of code. Since C++ likes to do type-checking, you must specify what the return value and arguments of your function pointer. Let's look at the syntax for creating a function pointer to a function which takes an integer and returns an integer.

int *f(int); // returns an int*
int (*f)(int); // This is a pointer to a function
Note that the parenthesis are required. The first line declares a function that returns a pointer to an integer and takes an integer as an argument. The second line makes f a pointer to a function that takes an integer and returns an integer.

Now, let's say in our code we create a function abs, which returns the absolute value of a function:

int abs(int num)
{ return num < 0 ? -num : num; }
Now, if we want f to point to the function abs, the syntax is simple:
f = abs;
If you refer to a function without parenthesis, you are getting the function to that pointer. So this line of code assigns the address of the function abs to the pointer f. Now, if we want to call abs using f, we just use similar syntax as to if we were just calling abs:
f(-4); // returns 4
(*f)(-4); // returns 4
Either of these statements are correct. The latter one first dereferences f, then calls it with parameter -4. The second bypasses the dereferencing. Recall that abs is a pointer, and to execute abs, you simply need to use the parenthesis, passing in a value. The parenthesis tell the compiler that you want to branch execution to the function's address using the parameters you supply within the parenthesis. So, since f points to abs (which is a pointer itself), you can just use the parenthesis (as in line 1 above) and the result is the same as if you first dereferenced f to get abs, then used the parenthesis to call the function. If you understand this, you are in great shape!

This gets really cool once you start doing things like having an array of function pointers. Say you write code that does this:

        int a(int);

        int b(int);

        int c(int);



        typedef int (*pFunction)(int);

        pFunction funcArray[3] = {a, b, c};



        funcArray[1](5);  // equiv. to b(5);

        funcArray[0](10); // equiv. to a(10);



        // equiv. to c(b(a(10)));  Neat looking code!

        funcArray[2](funcArray[1](funcArray[0](10)));

Pretty neat stuff, eh? The typedef statement creates a type named pFunction which is a pointer to a function that returns an integer and takes a single integer as an argument. The rest is (hopefully) fairly self-explanatory.

top