Lab Exercise 8: Functions
Objective
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.
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 Lab08.X folder.
Select Open Project
Open C Source File
Open the Lab08.c source file from the project tree by double-clicking on its icon.
This brings up Lab08.c in a window to edit.
Edit Source File
Write two function prototypes based on the following information:
- 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().
- Pass the variables intVariable1 and intVariable2 to multiply_function().
- Store the result of multiply_function() in the variable product.
- Pass the variables floatVariable1 and floatVariable2 to divide_function().
- Store the result of divide_function() in the variable quotient.
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 button. This builds and sends the program to the simulator.
Click on the Continue button. This begins the simulation.
Click on the Halt button. This stops execution so that we may examine the variables and their values. Open the Variables window with either Window > Debugging > Variables or pressing on Alt + Shift + 1.
Results
After building, executing, and stopping your code, the results of the operations appear in the Variables window as shown in the accompanying image.
intVariable2 = 0x28 = 40
product = 0x3E8 = 1000
floatVariable1 = 17.78690
floatVariable2 = 30.12345
quotient = 0.5904669
intQuotient = 0
End Debug Session
End the Simulation Session by clicking the Finish Debugger Session button.
Close the Project.
Analysis
Lines 24-26
STEP 3.1:
Step 3.1 creates the function prototypes for the functions you will write below. The prototypes are required to inform the compiler of the proper format of a function call to these functions so that when it encounters them in the main code before they have actually been defined, it knows that it is not a mistake.
float divide_function( float x, float y );
Lines 72-75
STEP 3.2:
Step 3.2 makes the calls to the functions from within the main routine.
quotient = divide_function( floatVariable1 , floatVariable2 );
Since both functions return a value, the proper way to call them is by assigning their results to variables. The variables passed as parameters are defined and initialized higher up in the code.
Lines 98-101
STEP 3.3:
Step 3.3 requires you to write the multiply_function() itself. The framework of the function is already there, all you need to do is write the function header (based on the prototype you wrote in step 1 and write the body which requires you to return only the product of the two parameters. The whole function should look like:
{
return (x * y);
}
Lines 117-120
STEP 3.4:
Step 3.4 is almost identical to step 3, but this time you have to write the divide_function(). The structure of the function is the same:
{
return (x / y);
}
This function returns a floating point value and takes two floating point parameters. Other than that and the mathematical operation carried out in the body, it is essentially identical to the multiply_function().
Conclusions
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 the 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 results in larger, slower-running code. Over time, you need to develop a sense for when a function makes sense, and when in-line code is a better solution.