< previous page page_336 next page >

Page 336
0336-01.gif
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Function Prototype A function declaration without the body of the function.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Function Definition A function declaration that includes the body of the function.
Whether we are talking about functions or variables, the general idea in C++ is that a declaration becomes a definition if it also allocates memory space for the item. For example, a function prototype is merely a declarationthat is, it specifies the properties of a function: its name, its data type, and the data types of its parameters. But a function definition does more; it causes the compiler to allocate memory for the instructions in the body of the function. (Technically, all of the variable declarations we've used so far have been variable definitions as well as declarationsthey allocate memory for the variable. In Chapter 8, we see examples of variable declarations that aren't variable definitions.)
The rule throughout C++ is that you can declare an item as many times as you wish, but you can define it only once. In the NewWelcome program, we could include many function prototypes for PrintLines (though we'd have no reason to), but only one function definition is allowed.
Function Prototypes
We have said that the definition of the main function usually appears first in a program, followed by the definitions of all other functions. To satisfy the requirement that identifiers be declared before they are used, C++ programmers typically place all function prototypes near the top of the program, before the definition of main.
A function prototype (known as a forward declaration in some languages) specifies in advance the data type of the function value to be returned (or the word void) and the data types of the parameters. A prototype for a void function has the following form:

 
< previous page page_336 next page >