| Previous | Table of Contents | Next |
A function prototype tells the compiler the return type, name, and parameter list. Functions are not required to have parameters, and if they do, the prototype is not required to list their names, only their types. A prototype always ends with a semi-colon (;).
A function definition must agree with its prototype in return type and parameter list. It must provide names for all the parameters, and the body of the function definition must be surrounded by braces. All statements within the body of the function must be terminated with semicolons, but the function itself is not ended with a semicolon; it ends with a closing brace.
If the function returns a value, it should end with a return statement, although return statements can legally appear anywhere in the body of the function.
Every function has a return type. If one is not explicitly designated, the return type will be int. Be sure to give every function an explicit return type. If a function does not return a value, its return type will be void.
Here are some examples of function prototypes:
long FindArea(long length, long width); // returns long, has two parameters void PrintMessage(int messageNumber); // returns void, has one parameter int GetChoice(); // returns int, has no parameters BadFunction(); // returns int, has no parameters
Here are some examples of function definitions:
long Area(long l, long w)
{
return l * w;
}
void PrintMessage(int whichMsg)
{
if (whichMsg == 0)
cout << Hello.\n;
if (whichMsg == 1)
cout << Goodbye.\n;
if (whichMsg > 1)
cout << Im confused.\n;
}
![]() | Not only can you pass in variables to the function, but you also can declare variables within the body of the function. This is done using local variables, so named because they exist only locally within the function itself. When the function returns, the local variables are no longer available. |
Local variables are defined like any other variables. The parameters passed in to the function are also considered local variables and can be used exactly as if they had been defined within the body of the function. Listing 5.2 is an example of using parameters and locally defined variables within a function.
Listing 5.2 Demonstrates Use of Local Variables and Parameters
1: #include <iostream.h>
2:
3: float Convert(float);
4: int main()
5: {
6: float TempFer;
7: float TempCel;
8:
9: cout << Please enter the temperature in Fahrenheit: ;
10: cin >> TempFer;
11: TempCel = Convert(TempFer);
12: cout << \nHeres the temperature in Celsius: ;
13: cout << TempCel << endl;
14: return 0;
15: }
16:
17: float Convert(float TempFer)
18: {
19: float TempCel;
20: TempCel = ((TempFer - 32) * 5) / 9;
21: return TempCel;
22: }
![]() | Please enter the temperature in Fahrenheit: 212 Heres the temperature in Celsius: 100 Please enter the temperature in Fahrenheit: 32 Heres the temperature in Celsius: 0 Please enter the temperature in Fahrenheit: 85 Heres the temperature in Celsius: 29.4444 |
![]() | On lines 6 and 7, two float variables are declared, one to hold the temperature in Fahrenheit and one to hold the temperature in degrees Celsius. The user is prompted to enter a Fahrenheit temperature on line 9, and that value is passed to the function Convert(). |
Execution jumps to the first line of the function Convert() on line 19, where a local variable, also named TempCel, is declared. Note that this local variable is not the same as the variable TempCel on line 7. This variable exists only within the function Convert(). The value passed as a parameter, TempFer, is also just a local copy of the variable passed in by main().
This function could have named the parameter FerTemp and the local variable CelTemp, and the program would work equally well. You can reenter these names and recompile the program to see this work.
The local function variable Tempcel is assigned the value that results form subtracting 32 from the parameter TempFer, multiplying by 5, and then dividing by 9. This value is then returned as the return value of the function, and on line 11 it is assigned to the variable TempCel in the main() function. It is printed on line 13.
The program is run three times. The first time the value 212 is passed in to ensure that the boiling point of water in degrees Fahrenheit (212) generates the correct answer in degrees Celsius (100). The second test is the freezing point of water. The third test is a random number chosen to generate a fractional result.
As an exercise, try reentering the program with other variable names as illustrated here:
1: #include <iostream.h>
2:
3: float Convert(float);
4: int main()
5: {
6: float TempFer;
7: float TempCel;
8:
9: cout << Please enter the temperature in Fahrenheit: ;
10: cin >> TempFer;
11: TempCel = Convert(TempFer);
12: cout << \nHeres the temperature in Celsius: ;
13: cout << TempCel << endl;
14: return 0;
15: }
16:
17: float Convert(float Fer)
18: {
19: float Cel;
20: Cel = ((Fer - 32) * 5) / 9;
21: return Cel;
22: return 0;
23: }
You should get the same results.
![]() | A variable has scope, which determines how long it is available to your program and where it can be accessed. Variables declared within a block are scoped to that block; they can be accessed only within that block and go out of existence when that block ends. Global variables have global scope and are available anywhere within your program. |
![]() | Variables defined outside of any function have global scope and thus are available from any function in the program, including main(). |
| Previous | Table of Contents | Next |