Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good function pointer analogies?

The best one I have come up with so far is that a card catalog contains instructions on finding how to cook different types of food. It does not contain the information on how to cook the food, nor the instructions on cooking the food itself, but it can point you to relevant instructions which are located elsewhere. It is a good analogy because it demonstrates the flexibility on being able to cook different types of food (Mexican, Vegetarian, Indian, etc.) but the notion of a card catalog is quickly being replaced with computerized searches. I have been asked the dreaded "What is a card catalog?" Using Google as an example of finding cooking instructions is OK but there is not that clear distinction of going to a physical location, finding the instructions, and "executing them". Any one have any analogies that worked for them?

like image 275
ojblass Avatar asked May 24 '09 15:05

ojblass


2 Answers

I don't think it's a good idea to try and teach a very technical concept through non-technical analogies - presumably the people you are teaching do NOT need a vague "management summary" kind understanding, they will eventually need to apply the concept concretely, so why such a detour?

Why not explain it with a simple programming example? Perhaps the best one would be a generic sorting algorithm (presumably the students have already learned about sorting).

"A function pointer is basically a way to pass a piece of code around to be called by other code who does not need to know about it beforehand. For example, a sorting algorithm like quicksort or mergesort really only needs to know one thing about the things it's sorting: how to compare them. But if you build the comparison into the sorting algorithm, it will only be able to compare one thing, like ints. By passing the comparison logic as a function pointer to the sorting algorithm, to be called whenever the algorithm needs to compare elements, the algorithm can now be used to sort anything at all without having to change its code - if you want to use it to compare apples and oranges, you only need to write a fruit-comaprison function and pass a pointer to it to the sorting algorithm, along with your fruit to be sorted."

like image 114
Michael Borgwardt Avatar answered Dec 08 '22 22:12

Michael Borgwardt


That analogy seems overly complex.

A function pointer is just like a phone number of someone who can get the job done for you.

like image 22
Assaf Lavie Avatar answered Dec 08 '22 23:12

Assaf Lavie