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.
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
Open the ProjectStart MPLAB® X, 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 Lab08.X folder.Select Open Project .
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)
Once you finish writing the code: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.
Click on the Halt 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 SessionEnd the Simulation Session by clicking the Finish Debugger Session 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.