Sams Teach Yourself C++ in 21 Days

(singke) #1
Organizing into Functions 105

5


Also note that the parameter names in the prototype are lengthand width, but the para-
meter names in the definition are lenand wid. As discussed, the names in the prototype
are not used; they are there as information to the programmer. It is good programming
practice to match the prototype parameter names to the implementation parameter names,
but as this listing shows, this is not required.
The arguments are passed in to the function in the order in which the parameters are
declared and defined, but no matching of the names occurs. Had you passed in
widthOfYard, followed by lengthOfYard, the FindArea()function would have used the
value in widthOfYardfor length and lengthOfYardfor width.

The body of the function is always enclosed in braces, even when it consists
of only one statement, as in this case.

NOTE


Execution of Functions ........................................................................................


When you call a function, execution begins with the first statement after the opening
brace ({). Branching can be accomplished by using the ifstatement. (The ifand other
related statements will be discussed on Day 7, “More on Program Flow.”) Functions can
also call other functions and can even call themselves (see the section “Recursion,” later
today).
When a function is done executing, control is returned to the calling function. When the
main()function finishes, control is returned to the operating system.

Determining Variable Scope ................................................................................


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’s braces and “go out of existence” when that
block ends. Global variables have global scope and are available anywhere within your
program.

Local Variables ..............................................................................................


Not only can you pass in variables to the function, but you also can declare variables
within the body of the function. Variables you declare within the body of the function are
called “local” because they exist only locally within the function itself. When the func-
tion returns, the local variables are no longer available; they are marked for destruction
by the compiler.
Free download pdf