Function Pointer

◀ Generality in Functions▶ Inline Function
Amazon I did not know that I could pass a function pointer as an argument of another function until my sophomore year in college. At first I was very excited because I thought it was something very useful, but to my disappointment, it never really proved useful and I hardly ever used it in any of the programs I wrote.

However when I started working got a software company I actually saw many instances of function pointer in the code base I was working with.

Let’s discuss this topic. In essence, a function can use a function pointed to by the function pointer. The signature of a function pointer looks like this:
/* 
ptf is a pointer that points to a function that takes one char argument and returns an int
*/
int (*ptf)(char);
Let me show you a sample program that illustrates the use of function pointers:
#include<iostream>
#include<cctype>	/* or <ctype.h> */
using namespace std;

int mike(char);
int vivien(char);
void analyze(int (*ptf)(char), char);

int main() {
	char grade;

	cout << "What is the grade you expect to get? ";
	cin >> grade;
	grade = tolower(grade);
	while(grade!=\'a\' && grade!=\'b\' && grade!=\'c\' && grade!=\'d\' 
		&& grade!=\'f\') {
		cout << "Not a valid grade. Please enter again: ";
		cin >> grade;
		grade = tolower(grade);
	}
	cout << "Mike thinks that ";
	analyze(mike, grade);
	cout << " because Mike is dumb dumb...\n";
	cout << "Vivien thinks that ";
	analyze(vivien, grade);
	cout << " because Vivien is super smart!\n";
	return 0;
}

int mike(char grade) {
	int diff = \'f\' - grade;
	return diff * 10;
}

int vivien(char grade) {
	int diff = \'f\' - grade;
	return diff * 2;
}

void analyze(int (*ptf)(char), char grade) {
	cout << "it will take " << (*ptf)(grade);
	cout << " hours every week";
}
As you can see, analyze() simply takes the name of a function (with the same argument types and return type) as the first argument, and then uses its second argument to call that function. Inside main(), when you want to call analyze() you pass the function address and the corresponding argument.

The name of the function, as you may or may not yet realize, is actually the address of that function, allowing you to identify that function. You should be able to follow pretty much everything else in this program.

A subtle point is that not only do you need to pass the function name, you also need to know its arguments to be able to use that function. You don’t need to get the arguments to ptf() from the call to analyze() if you can get them somewhere else.
The primary application of using function pointer is to indicate how you would like to handle data.

For example the STL container class, set, sorts its elements in the ascending order. You can change the sorting function, or the comparison function, by creating a function and referencing it through a function pointer in the set’s constructor. Then the set will use that function to compare the elements inserted to the set and sort them accordingly.

Another application of using pointers to non-member functions is to satisfy a callback function requirement. In places where a function is given to be called whenever some conditions hold true we need to use function pointer.

A case in point is you are writing a server application and just before the server shuts down it needs to do cleanup tasks. You can put those tasks inside a function and hook it to the shut down event of the server as a callback function, so that it is always invoked just before the server shuts down.
Another scenario in which function pointers may come in handy is that at some point you do not know which function you need to call because it, for example, depends on the user input. The result of calling that function may need further processing.

In this scenario, you may want to write a master function that takes another function as an argument. Depending on the user input, the call to the master function uses the appropriate function. Then the master function can get the results and process it accordingly.

However, as you probably already see, you can simply use if-else statements to call the appropriate function, and then pass the results to another function to do more stuff.
◀ Generality in Functions▶ Inline Function

fShare
Questions? Let me know!