Wyo C++ - Ch. 9 Notes
Objective #1: Learn how to build structured programs that are divided into
functions.
Objective #2: Understand what is meant by the phrase "scope of
variables."
- Variables are either global
or local in nature. Global variables
are ones that are declared outside and above
the main function. They can be used in any function throughout the
program. It is not wise to use global variables any more than you have to.
One small error (for example, miscalculating a value) could lead to multiple,
hard-to-find errors in large programs. Local variables are ones that are declared
inside of a function, including main. They cannot be used or referred to in
other functions.
- In order to strengthen your program and functions' autonomy and encapsulation,
you should avoid the use of global variables and try to use local variables
instead.
- The scope
of a variable is the area in which it can be legally referenced. A global
variable's scope is the whole program. A local variable's scope is the function
in which it is declared.
- In the example program above (with the function printMyName), the variable
userInput is a local variable which belongs to the main function. The variable
i is a local variable to the function printMyName. (numOfTimes isn't really
a variable. It is a parameter technically. See objective 3 for more details
on parameters.)
Objective #3: Understand how data is passed to functions.
- Data is passed to functions as arguments
(sometimes referred to as actual
parameters). When a function is "called"
by the main function (or another function), one or more arguments are passed
to the function. On the receiving end, the function accepts these arguments
(technically, as formal parameters). The variable names of the arguments (that
is, actual parameters) from the "calling" function do not have
to be the same as the names of the formal parameters in the "called"
function. But, the datatypes of the arguments and the parameters should match
exactly. An error could occur if the data types do not match.
- We type "return 0;" as the last statement in the main functions
of all the programs that we write in this C++course. This returns a message
(of "0") to the operating system indicating that our C++ program
ran successfully. A returned nonzero value is often used by programmers to
indicate that an error occurred during the program's execution.
Often, though, you want your function to return a computed value to the calling
function. This value is used in the exact statement that the function was
called. For example,
fahrenheit = celsiusToFahrenheit(celsius);
sends the argument, celsius, to the function, celsiusToFahrenheit, where
it is used to determine the equivalent Fahrenheit temperature. Then, the
final value is returned from within the function to the main function where
it is assigned to the fahrenheit variable.
- There are technically three ways to pass data as arguments to functions.
You can pass data by value, by reference, or by address.
- passing by value
is the preferred method. You simply use a variable name, an actual numeric
literal, or an expression in the parentheses of the call statement. (see
p. 165) This method is the safest because it does not actually change
the value of the argument in the calling function.
In the example below, the argument named price is passed by value to the
function addTax.
#include <iostream.h>
double addTax(double);
int main()
{
double price = 10.00;
double finalPrice = 0.0;
finalPrice = addTax(price);
cout << "The final price with tax is "
<< finalPrice << endl;
return 0;
}// end of main
double addTax(double initialPrice)
{
return (initialPrice + initialPrice * 0.06);
}// end of addTax
- passing by reference
is to be used when you want the function to actually and permanently change
the values of one or more variables. This method is dangerous but beneficial
sometimes since it allows you to affect the values of arguments in your
calling function. You would purposefully pass by reference if you want
to return 2 or more values to the calling function. Since it is not possible
in C++ to execute two return statements within a function and since it
is not possible to return two values in the same return statement (i.e.
return myResult, myOtherResult is illegal), passing by reference can allow
you to modify two or more variables in the calling function (i.e. main).
Also, if you need to pass a really large variable or object, it saves
memory to pass by reference. Since a copy of the variable is not being
made when you pass by reference, less memory overhead is required. For
int, double, and char variables, passing by reference to save memory is
not really necessary. However, if you were to pass a large "object"
such as a piece of clipart or a whole file, you would intentionally pass
by reference.
You must use an ampersand (&) before the formal parameter names in
the function header (the first line of the function definition)
to denote passing by reference.
In the example below, the argument named price is passed by reference
to the function addTax.
#include <iostream.h>
void addTax(double &);
int main()
{
double price = 10.00;
addTax(price);
cout << "The final price with tax is "
<< price << endl;
return 0;
}// end of main
void addTax(double & incomingPrice)
{
incomingPrice = incomingPrice + incomingPrice * 0.06;
}// end of addTax
- passing by address
is technically what happens when you pass an array to a function. Since
arrays are actually pointers to one memory address, you are not
passing the actual array but just information that tells the function
where the first element of the array is stored in the RAM of the computer.
Making changes to the array within the function will affect the
values within the array permanently, so this method should be used with
care.
Objective #4: Learn how to use the library functions that are included with the
compiler.
- There are many functions available to C++ programmers which were written
by other programmers. You can use the #include
compiler directive at the top of your program to take advantage of these functions.
The source code for those functions does not need to be included within your
program. In fact, you do not even have to know exactly how they work. You
do have to know how many arguments to send to the functions and what datatypes
to use for those functions. However, some of the "older" header
functions end with a ".h" extension. For example, there are many
mathematical library functions that can be used in your program if you "include"
the math.h
(see pp. 171 & 172) header file at the top of your program. Similarly,
the ctype.h
header file may be useful as well (see p. 172).
Objective #5: List and explain the four basic types of statements.
(This objective is not from our textbook.)