Lab Exercise 8: Functions


The purpose of this lab is to illustrate the creation and use of functions. Functions help promote modular, more organized code. Functions are a major part of the C language in the form of the standard C library, of which printf() is a member. You may also create your own functions to promote code reuse as well as make your programs more readable and more easily maintained.

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files



Open the Project

Start MPLAB® X, then click on the Open Project Main_Open_Project.png icon on the main toolbar

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab08.X folder.

Select Open Project OpenProjectButton.png.


Open C Source File


Open the Lab08.c source file from the project tree by double clicking on its icon.

This will bring up Lab08.c in a window to edit


Edit Source File

Write two function prototypes based on the following information:

1. Function Name: multiply_function()

  • Parameters: int x, int y
  • Return Type: int

2. Function Name: divide_function()

  • Parameters float x, float y
  • Return Type: float

Call the multiply_function() and the divide_function().

(a) Pass the variables intVariable1 and intVariable2 to multiply_function()
(b) Store the result of multiply_function() in the variable product
(c) Pass the variables floatVariable1 and floatVariable2 to divide_function()
(d) Store the result of divide_function() in the variable quotient

Write the function multiply_function(). Use the function prototype you created in step 1 as the function header. In the body, all you need to do is return the product of the two input parameters: (x * y)

Write the function divide_function(). Use the function prototype you created in step 1 as the function header. In the body, all you need to do is return the quotient of the two input parameters (x / y)



Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.
Click on the Halt Debug_Pause.png button. This will stop execution so that we may examine the variables and their values.

Open the Variables Window with either Window -> Debugging -> Variables or ( Alt + Shift + 1)



End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug_Finish_Debugger_Session.png button.

Close the Project.


While the functions you created in this exercise were relatively trivial, they do illustrate the syntax and basic mechanism. Functions can be very useful for making code more modular, by taking blocks of code that have a single, well defined purpose and separating them from the main block of code. This has several advantages. First, it makes your code easier to understand and manage. Second, it makes debugging easier because it allows you to have blocks of known good code in separate files (more on this in the next lab). Third, it helps promote code reuse. If you write your functions properly, they can be used over and over again in your future programs.

However, you should be aware that functions can generate extra overhead. While functions can be very useful, and in many cases reduce the amount of code generated, there are situations where overusing functions will result in larger, slower running code. Over time, you will need to develop a sense for when a function makes sense, and when in-line code is a better solution.

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.