Function pointer vs switch cases
The question at hand is to discuss the use of function pointers as a replacement for using switch cases. Here is my take.
There are three levels of simple transfer commands aka gotos. They are:
A function invocation is a variable goto with the caveat that control must be returned to the calling function after the called function. In C the code within the called function has a different scope than the scope within the calling function. This is also true in most other languages although there are many caveats.
Now what does this have to do with function pointers and switch cases? Function pointers provide a way to get around using switches. Instead of transferring control to a case label we invoke a function that is the value of variable containing a function pointer. Instead of having a list of (value,label) pairs we have a list of (value,function pointer) pairs.
We can express this list in many ways, depending on what the values are. Unlike the switch cases the values don’t have to be integers. If they are the integers in the range 0:n-1 the list can be an array of function pointers and instead of having a switch statement and a list of cases with their code we have something like
func[i](args);On the other hand if the values are strings we could have an array of (key,function) pairs, search the array for the key, and invoke the corresponding function.
The great advantage of this sort of thing is that we can replace large switch statements within a single function with a series of small functions, each handling a single case. The resulting code (may be) is more compact and more readable.
However there are some gotchas. The two big ones are scope and signature.
In a switch block the code within the cases is within the scope of the containing function. What this means is that the code within the case has access to the variables in the function containing the switch. When you use the function pointer technique, the called functions don’t automatically have access to the variables of the calling function. (You can fake it by passing them through the calling sequence.) As a result the called functions may have to do a bunch of makeup work.
The other gotcha is the signature problem. As a practical matter all of the functions in the (value,function) list must have the same prototype (signature) because they all are going to be called the same way. What this means is that these functions ususally have to be specially written within the context of being equivalent to replacing a switch.
This page was last updated August 1, 2008.