Lab Exercise 13: Function Pointers
Objective
This demo provides a working example of function pointers in action. Function pointers are not frequently used in C programming (perhaps due to their strange syntax), but can be extremely useful in some circumstances.
Reference Materials
Exercise Files
Procedure
Open the Project
Start MPLAB® X IDE, then click on the Open Project icon on the main toolbar. Navigate to the folder where you saved the exercise files for this class.
Click on the Lab13.X folder.
Select Open Project .
Debug Project
Click on the Debug Project button. This will build and send the program to the simulator.
Click on the Continue button. This begins the simulation. Wait for the UART1 Output Window to finish outputting.
Click on the Halt button. This will stop execution so that we may examine the results.
What just happened?
As was done earlier in the class, we opened a pre-configured MPLAB X IDE workspace with a complete, working program. We then compiled the code and ran it long enough for it to complete its task. This program uses a function pointer to pass the address of a mathematical function to another function that will compute its integral.
The integral example was adapted from one published on Wikipedia at: https://en.wikipedia.org/wiki/Function_pointer. The integral function takes three parameters: the upper and lower bounds of the integral, and the address of the function that it is to evaluate. The function’s header looks like:
Note that the third parameter is defined as a function pointer. When we call this function, we only need to provide the name of the function we want to integrate. For example:
The function xsquared() is a simple mathematical function defined as:
{
return (x * x);
}
There are other functions that may be passed to the integral() function as well.
Results
The program evaluates the integral of three functions: y=x, y=x2 and y=x3. After running the program you should see the following printed out in the Sim UART1 window:
Code Analysis
Lines 35, 41, and 47
These three lines make calls to the integral() function. Each one passes a different function’s address to the integral() function for evaluation. The address of a function is represented by the function’s name alone (no parentheses or parameters).
y2 = integral(0, 1, xsquared);
y3 = integral(0, 1, xcubed);
Lines 60-63
This function justx() simply returns the value of x (y = x)
{
return x;
}
Lines 72-75
This function xsquared() simply returns the value of x2 (y = x2)
{
return (x * x);
}
Lines 84-87
This function xcubed() simply returns the value of x3 (y = x3)
{
return (x * x * x);
}
Lines 99-112
This is the integral() function which will evaluate the integral of any mathematical function passed to it over the range specified by the lower bound a and the upper bound b. The third parameter of the function header is a function pointer. It expects to receive the address of a function, which may be passed simply as the name of a function.float integral(float a, float b, float (*f)(float)).
float sum = 0.0;
float x;
int n;
//Evaluate integral{a,b} f(x) dx
for (n = 0; n <= 100; n++)
{
x = ((n / 100.0) * (b-a)) + a;
sum += (f(x) * (b-a)) / 101.0;
}
return sum;
}
The algorithm used to evaluate the integral is beyond the scope of this course. However, it should be noted that the function name that is passed to this function is accessed via its parameter name f. For example, the line that states:
invokes the function passed via f, and passes the parameter x to it. So, when the function xsquared() is passed, f( x ) evaluates xsquared(x), where x is a local variable defined within integral() and is defined in the line immediately above.
End Debug Session
Clear the UART 1 Window - put the cursor in the UART 1 Window then enter Ctrl +L. This will clear the UART 1 Window before your next simulation.
End the Simulation Session by clicking the Finish Debugger Session button.
Then CLOSE the Project by right-clicking on Lab02 from the Projects Window and then selecting Close.
Conclusions
While not frequently used, function pointers can provide a very convenient mechanism for passing a function to another function.
Many other possible applications exist
- Jump tables
- Accommodating multiple calling conventions
- Callback functions (used in Windows™)
- Call different versions of a function under different circumstances