|
|
|
|
|
|
|
Notice that the function heading does not end in a semicolon the way a function prototype does. It is a common syntax error to put a semicolon at the end of the line. |
|
|
|
|
|
|
|
|
The syntax of the parameter list differs slightly from that of a function prototype in that you must specify the names of all the formal parameters. Also, it's our style preference (but not a language requirement) to declare each formal parameter on a separate line: |
|
|
|
|
|
|
|
|
Because a function body is a block, any functionnot only the main functioncan include variable declarations within its body. These variables are local variables because they are accessible only within the block in which they are declared. As far as the calling code is concerned, they don't exist. If you tried to print the contents of a local variable from another function, a compile-time error such as UNDECLARED IDENTIFIER would occur. You saw an example of a local variable in the NewWelcome programthe count variable declared within the PrintLines function. |
|
|
|
 |
|
 |
|
|
Local Variable A variable declared within a block and not accessible outside of that block. |
|
|
|
|
|
|
|
|
In contrast to local variables, variables declared outside of all the functions in a program are called global variables. We return to the topic of global variables in Chapter 8. |
|
|
|
|
|
|
|
|
Local variables occupy memory space only while the function is executing. At the moment the function is called, memory space is created for its local variables. When the function returns, its local variables are destroyed.* Therefore, every time the function is called, its local variables start out with their values undefined. Because every call to a function is independent of every other call to that same function, you must initialize the local variables |
|
|
|
 |
|
 |
|
|
*We'll see an exception to this rule in the next chapter. |
|
|
|
|
|