|
|
|
![]() | Section 5: Functions |
Now that you know what a function is, let's look at function syntax. We've already seen that a function can take some inputs, do some stuff, and then produce an output.
The basic form of a function definition is this:
output function_name ((input_1, input_2, input_3, input_...) {
// code to execute inside function
}
It's called a function definition because we
are defining the function. We are saying, "This is a
function named function_name, whose inputs are
input_1, input_2, etc., and whose
output is output. When it is called, the function
will execute the code in between its curly braces ({}).
At this point, let's refine our sample function definition. When programmers talk about functions, instead of the word input they usually use the word parameter. A parameter to a function is nothing more than an input to a function. At the same time, instead of using the word output, programmers generally refer to the return of a function. A particular function "returns" a value. So, here is our updated function definition:
return_type function_name ((parameter_1, parameter_2, parameter_3, parameter_...) {
// code to execute inside function
}
Notice that in place of output, the function
definition says return_type. That's because when we
are actually writing a function definition, we'll put the
return type there, immediately preceding the name of the
function. The return type is nothing more than a plain old
variable type, such as int, or double, etc.
Similarly, parameters use variable types also. If the first
input to a function is an int, then the first
parameter will be something like int my_number.
We'll see what my_number does in just a moment.
Enough dilly-dally, let's see a real, working, C++ function that actually does something! Suppose we need a function that, converts a temperature from Celsius to Fahrenheit. Here it is:
double
convertToFahrenheit(double celsius){
double fahrenheit = ((9/5) * celsius) + 32;
return fahrenheit;
};
Seems simple enough. Now let's put together a simple program that outputs some corresponding values for fahrenheit and celsius.
//include this file for cout
#include <iostream.h>
// function for changing the value
// of celsius to fahrenheit
double
convertToFahrenheit(double celsius){
double fahrenheit = ((9/5) * celsius) + 32;
return fahrenheit;
}
void
convertToFahrenheitAndPrint(double celsius){
double fahrenheit = convertToFahrenheit(celsius);
cout << "Celsius: " << celsius;
cout << " Fahrenheit: " << fahrenheit << endl;
}
int main() {
// boiling point
cout << "Boiling Point of Water: ";
convertToFahrenheitAndPrint(100);
// normal body temperature
cout << "Normal body temperature: ";
convertToFahrenheitAndPrint(37);
// freezing point of water
cout << "freezing point of water: ";
convertToFahrenheitAndPrint(0);
return 0;
}
I have included another function called convertToFahrenheitAndPrint. This function takes as a parameter a double (decimal number), and prints "celsius:" then the number (the celsius value), then "fahrenheit:" then the converted value for fahrenheit that it gets from the function convertToFahrenheit.
Try it out. Do you notice a bug? If you're not up on Celsius Fahrenheit conversions the right output is as follows:
Boiling Point of Water: Celsius: 100 Fahrenheit: 212 Normal body temperature: Celsius: 37 Fahrenheit: 98.6 freezing point of water: Celsius: 0 Fahrenheit: 32
If you typed in the above program as listed the results that you get are:
Boiling Point of Water: Celsius: 100 Fahrenheit: 132 Normal body temperature: Celsius: 37 Fahrenheit: 69 freezing point of water: Celsius: 0 Fahrenheit: 32
Congratulations! You've found you're first bug. So what's going on here? My college physics book says that to convert from Celsius to Fahrenheit you multiply the Celsius value by 9/5 and then add 32. Looking at the incorrect results, I've noticed that the difference between the Celsius value and the Fahrenheit value is always 32. Then I remember something! Look at the line that does the conversion:
double fahrenheit = ((9/5) * celsius) + 32;Here's what's going on: When the computer does the 9/5 in parentheses it does integer division. Meaning it throws out the remainder. So 9/5 = 1. That is exactly how our program is behaving. It has been multiplying the celsius value by 1 and then adding 32. So How do we get the computer to do floating-point division? (that is, division that keeps remainders as decimal places). We have to let the computer know that the 9 and the 5 are not integers but are double's (double stands for double precision floating point number) So by changing the above line to:
double fahrenheit = ((9.0/5.0) * celsius) + 32;We have now fixed our bug! Try it and see if you get the correct answers.
|
|
|