Function Pointers in “C++” can be a little daunting at first for many people mostly because of their alien-looking syntax, but when you look at them more closely you will find out that it's very simple, straightforward and important concept of c++.
Function Pointers essentially allows you to pass and store functions to variables or in other functions for later use by some other part of the code, at some later point in time. More formally a function pointer is a special type of variable that can point to any function having the same function signature. This is a very powerful tool and we will explore it briefly later in this article. But first, let’s jump straight into the code and get our hands dirty.
int (* function_pointer)(int a , int b);
think of it like a normal c++ function where the first int is the return type of the function signature it can point to, (* function_pointer) is how we tell the c++ compiler that its a function pointer, you can name it whatever you’d like but (*) is important. Then the next part (int a, int b) are the arguments of the function signature which it can point to. That’s it you have now created your very first function pointer in c++.
With the syntax out of the way, let’s see why this is so powerful, in the above example we are passing an adder function to the function pointer variable, but this can be any function. You can write a multiply function, a division function, or a function that maybe adds the arguments first and then applies some kind of percentage on it, at the end of the day the function pointer will just call it.
Let’s take the following example, suppose you are developing this c++ library for a company, which manages their employee's data and calculates bonuses for each employee based on certain conditions. The company also specifies that they have multiple departments and for each department, they would like different criteria for calculating bonuses. After taking these requirements you do some planning start coding. Your library has this core function that performs the following tasks.
1) Takes in data.
2) Parses it.
3) Cleans it.
4) Calculates bonus for each employee depending on the Department.
You Quickly notice that the core function is mostly similar in nature except the Last part, where you calculate the bonus. The problem is that the bonus calculation for different departments varies from each other. You can incorporate different scenarios as well but it will be very messy and not maintainable code. Also, what will you do if the company suggests altering the calculation of certain departments, maybe they don’t want to give a bonus to certain employees depending on their performance record, yes it will quickly become your favorite nightmare of all time, because the reality is, that this part of the function is subject to change quite often.
With function pointers you can simply pass in a small function that will contain the exact implementation of the Bonus calculations for a particular Department, you don’t have to write multiple branch statements to detect the type of department, etc, instead you will have neat and elegant bonus calculation tailored for each department.
Let's see a code example for the above scenario
Our library class with the core function will look something like this
As we can see after performing all the similar operations the, core_functions calls a user-provided function pointer to get the bonus for each employee. Let's see how the above class can be consumed
The main function is passing a custom function to the Demo_lib core_function, which will be used for bonus calculation, you can write multiple functions specific to your needs and conditions, and the Demo_lib will simply call it when it will reach the bonus calculation part of the core_function
So with the use of function pointers we now have the ability to tailor different bonus criteria for different employees for a number of departments
What we have seen today is the classic function pointers in c++.
c++11 has introduced modern function pointers which makes defining them little simpler but more importantly also allows you to pass Anonymous lambda functions into other function pointers instead of writing standalone functions, Lambda functions can be really helpful to pass in as they can be declared on the fly since you don’t need them anywhere else. I highly encourage you to look up them, as they are the modern successors of c++ function pointers. And the perfect stepping stone in your c++ journey.
Chao, Aamir Out
In the middle of difficulty lies opportunity.