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.
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
MPLAB® XC16
C Compiler |
| | | | |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Procedure
1
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 .3
Edit Source File
STEP 1:
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
STEP 2:
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.
STEP 3:
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).
STEP 4:
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).
4
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 here:
intVariable1 = 0x19 = 25
intVariable2 = 0x28 = 40
product = 0x3E8 = 1000
floatVariable1 = 17.78690
floatVariable2 = 30.12345
quotient = 0.5904669
intQuotient = 0
The variable intQuotient shows the result of dividing two floating point numbers and storing the result in an integer variable.
5
End Debug Session
End the Simulation Session by clicking the Finish Debugger Session button.Close the Project.
Analysis
Line numbers correspond to those in the provided solution file.
Lines 24-26
STEP 1:
Step 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.
int multiply_function( int x, int y);
float divide_function( float x, float y );
Remember — a function prototype is just the first line (header) of the function definition followed by a semicolon.
Lines 72-75
STEP 2:
Step 2 makes the calls to the functions from within the main routine.
product = multiply_function( intVariable1 , intVariable2 );
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:
Step 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:
int multiply_function(int x, int y)
{
return (x * y);
}
Lines 117-120
STEP 4:
Step 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:
float divide_function( float x, float y )
{
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.