Function Pointers
After declaring a function, you call it using a statement that includes the function name followed by input arguments enclosed in parentheses. For example, assume you have defined the function Square as:
You call the function Square in another statement by using the expression:
What if you want to make the function you call a dynamic value that is chosen at runtime? One way to do this is to use a function pointer. You can create a variable, set its value to a pointer representing the function Square, and call Square indirectly. For example:
The value of the variable f is a pointer to Square. Therefore, the expression f(3) is the same as Square(3).
See Use Variables.
You can only create pointers to functions that accept input arguments. If you had defined Square as:
the statement f=Square; assigns the number 9 to f instead of assigning a pointer to the function Square.
To create a pointer to a function that does not have input arguments, you first need to modify the function so it includes a dummy argument. For example: