| Previous | Table of Contents | Next |
When people talk about C++ they mention objects first. Yet objects rely on functions to get their work done. Today you will learn
A function is, in effect, a subprogram that can act on data and return a value. Every C++ program has at least one function, main(). When your program starts, main() is called automatically. main() might call other functions, some of which might call still others.
Each function has its own name, and when that name is encountered, the execution of the program branches to the body of that function. When the function returns, execution resumes on the next line of the calling function. This flow is illustrated in Figure 5.1.
Figure 5.1 When a program calls a function, execution switches to the function and then resumes at the line after the function call.
A well-designed function will perform a specific task. That means it does one thing, does it well, and then returns.
Complicated tasks should be broken down into multiple functions, and then each can be called in turn. This makes your code easier to understand and easier to maintain.
You will find throughout this book that I talk a lot about making your program easy to maintain. The big cost in programming is not writing the program, it is keeping it useful and reliable throughout its shelf-life.
Before you can use a function, you must first declare the function and then define the function.
![]() | The declaration tells the compiler the name, return type, and parameters of the function. The declaration of a function is called its prototype. |
![]() | The definition tells the compiler how the function works. No function can be called from any other function that hasnt first been declared. |
The built-in functions supplied by your compiler vendor will have their function prototypes already written. You just #include the appropriate file and youre all set.
![]() | The function prototype is a statement, which means it ends with a semicolon. It consists of the functions return type, name, and parameter list. |
The parameter list is a list of all the parameters and their types, separated by commas. Figure 5.2 illustrates the parts of the function prototype.
Figure 5.2 Parts of a function prototype.
The function prototype and the function definition must agree exactly about the return type, the name, and the parameter list. If they do not agree, you will get a compile-time error. Note, however, that the function prototype does not need to contain the names of the parameters, just their types. A prototype that looks like this is perfectly legal:
long Area(int, int);
This prototype declares a function named Area() that returns a long and that has two parameters, both integers. Although this is legal, it is not a good idea. Adding parameter names makes your prototype clearer. The same function with named parameters might be:
long Area(int length, int width);
It is now obvious what this function does and what the parameters are.
Note that all functions have a return type. Listing 5.1 demonstrates a program that includes a function prototype for the Area() function.
Listing 5.1 Shows a Function Declaration and the Definition and Use of That Function
1: // Listing 5.1 - demonstrates the use of function prototypes
2:
3: #include <iostream.h>
4: int FindArea(int length, int width); //function prototype
5:
6: int main()
7: {
8: int lengthOfYard;
9: int widthOfYard;
10: int areaOfYard;
11:
12: cout << \nHow wide is your yard? ;
13: cin >> widthOfYard;
14: cout << \nHow long is your yard? ;
15: cin >> lengthOfYard;
16:
17: areaOfYard= FindArea(lengthOfYard,widthOfYard);
18:
19: cout << \nYour yard is ;
20: cout << areaOfYard;
21: cout << square feet\n\n;
22: return 0;
23: }
24:
25: int FindArea(int l, int w)
26: {
27: return l * w;
28: }
![]() | How wide is your yard? 100 How long is your yard? 200 Your yard is 20000 square feet |
![]() | The prototype for the FindArea() function is on line 4. Compare the prototype with the definition of the function on line 25. Note that the name, the return type, and the parameter types are the same. |
If they are different, a compiler error is generated. In fact, the only required difference is that the function prototype ends with a semicolon and has no body.
Also note that the parameter names in the prototype are length and width, but the parameter names in the definition are l and w. It turns out that the names in the prototype are not used; they are there as information to the programmer. The arguments are passed in to the function in the order in which they are declared and defined.
The definition of a function consists of the function header and its body. The header is exactly like the function prototype, except that the parameters must be named and there is no terminating semicolon.
The body of the function is a set of statements enclosed in braces. Figure 5.3 shows the header and body of a function.
A function prototype tells the compiler the functions name, return value, and para-meters.
Figure 5.3 The header and body of a function.
return_type function_name ( [type [parameterName]]...);
The function definition tells the compiler how the function works.
return_type function_name ( [type parameterName]...)
{
statements;
}
| Previous | Table of Contents | Next |