Lab Exercise 13: Function Pointers

Last modified by Microchip on 2023/11/09 09:21

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 Main open project icon 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 Open Project Button.

Debug Project

Click on the Debug Project Debug project button button. This will build and send the program to the simulator.
Click on the Continue Continue button button. This begins the simulation. Wait for the UART1 Output Window to finish outputting.
Click on the Halt Pause button 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:

float integral(float a, float b, float (*f)(float))

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:

y2 = integral(0, 1, xsquared);

The function xsquared() is a simple mathematical function defined as:

float xsquared(float x)
{
return (x * x);
}

There are other functions that may be passed to the integral() function as well.

Results

UART 1 window showing the output

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).

y1 = integral(0, 1, justx);
y2 = integral(0, 1, xsquared);
y3 = integral(0, 1, xcubed);

Lines 60-63

This function justx() simply returns the value of x (y = x)

float justx(float x)
{
return x;
}

Lines 72-75

This function xsquared() simply returns the value of x2 (y = x2)

float xsquared(float x)
{
return (x * x);
}

Lines 84-87

This function xcubed() simply returns the value of x3 (y = x3)

float xcubed(float x)
{
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:

sum += (f(x) * (b-a)) / 101.0;

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 Debug 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