Chapter 6 Functions Gaddis

¡Supera tus tareas y exámenes ahora con Quizwiz!

The exit() function causes a program to terminate, regardless of which function or control mechanism is executing. A C++ program stops executing when the return statement in function main is encountered. When other functions end, however, the program does not stop. Control of the program goes back to the place immediately following the function call. Sometimes, rare circumstances make it necessary to terminate a program in a function other than main. To accomplish this, the exit function is used. When the exit function is called, it causes the program to stop, regardless of which function contains the call. Program 6-29

#include <cstdlib> exit(0);

A local variable is defined inside a function and is not accessible outside the function. They are hidden from the statements in other functions, which normally cannot access them Program 6.16

A global variable is defined outside all functions and is accessible to all functions in its scope.

By using parameters , you can design your own functions that accept data this way.

A parameter is a special variable that holds a value being passed into a function. Here is the definition of a function that uses a parameter: void displayValue(int num) { cout << "The value is " << num << endl; } Notice the integer variable definition inside the parentheses (int num) . The variable num is a parameter. This enables the function displayValue to accept an integer value as an argument.

This statement causes the function to end, and it sends the value of the result variable back to the statement that called the function. A value-returning function must have a return statement written in the following general format:

A value-returning function must have a return statement written in the following general format: return expression ; Program 6.12 Program 6.14

A global constant is a named constant that is available to every function in a program. Program 6-19

Because a global constant's value cannot be changed during the program's execution, you do not have to worry about the potential hazards that are associated with the use of global variables. Global constants are typically used to represent unchanging values that are needed throughout a program.

A function prototype eliminates the need to place a function definition before all calls to the function. NOTE: Function prototypes are also known as function declarations . Function prototypes are usually placed near the top of a program so the compiler will encounter them before any function calls. Program 6.5

Before the compiler encounters a call to a particular function, it must already know the function's return type, the number of parameters it uses, and the type of each parameter. WARNING! You must place either the function definition or either/the function prototype ahead of all calls to the function. Otherwise the program will not compile. Program 6.5

When an argument is passed into a parameter, only a copy of the argument's value is passed. Program 6.9

Changes to the parameter do not affect the original argument. As you've seen in this chapter, parameters are special-purpose variables that are defined inside the parentheses of a function definition. They are separate and distinct from the argument that are listed inside the parentheses of a function call. The values that are stored in the parameter variables are copies of the arguments. Normally, when a parameter's value is changed inside a function, it has no effect on the original argument.

A function's local variables exist only while the function is executing. This is known as the lifetime of a local variable

When the function begins, its local variables and its parameter variables are created in memory, and when the function ends, the local variables and parameter variables are destroyed. This means that any value stored in a local variable is lost between calls to the function in which the variable is declared.

It's possible for a function to have some parameters with default arguments and some without. For example, in the following function (which displays an employee's gross pay), only the last parameter has a default argument:

// Function prototype void calcPay(int empNum, double payRate, double hours = 40.0); // Definition of function calcPay void calcPay(int empNum, double payRate, double hours) { double wages; wages = payRate * hours; cout << fixed << showpoint << setprecision(2); cout << "Gross pay for employee number ";

Variables that are defined inside a function are called local variables.

After the variable definition, the parameter variables num1 and num2 are added, and their sum is assigned to the result variable.

Practice Program 6.3

Practice Program 6.3

Some functions simply perform one or more statements, which follows terminate. These are called void functions. The displayMessage function, which follows, is an example. void displayMessage() { cout << "Hello from the function displayMessage.\n"; }

The function's name is displayMessage . This name gives an indication of what the function does: It displays a message. You should always give functions names that reflect their purpose. Notice that the function's return type is void . This means the function does not return a value to the part of the program that executed it. Also notice the function has no return statement. It simply displays a message on the screen and exits.

When a function is called, the program may send values into the function.

Values that are sent into a function are called arguments . You're already familiar with how to use arguments in a function call.

WARNING! When passing a variable as an argument, simply write the variable name inside the parentheses of the function call.

Do not write the data type of the argument variable in the function call. For example, the following function call will cause an error: displayValue(int x); // Error! The function call should appear as displayValue(x); // Correct Program 6-7 ,

When used as parameters, reference variables allow a function to access the parameter's original argument. Changes to the parameter are also made to the argument.

Earlier you saw that arguments are normally passed to a function by value, and that the function cannot change the source of the argument. C++ provides a special type of variable called a reference variable that, when used as a function parameter, allows access to the original argument.

WARNING! Each parameter variable in a parameter list must have a data type listed before its name.

For example, a compiler error would occur if the parameter list for the showSum function were defined as shown in the following header: void showSum(int num1, num2, num3) // Error! A data type for all three of the parameter variables must be listed, as shown here: void showSum(int num1, int num2, int num3) // Correct

Functions may return true or false values.

Frequently there is a need for a function that tests an argument and returns a true or false value indicating whether or not a condition exists. Such a function would return a bool value. Program 6.15

A function may send a value back to the part of the program that called the function. You've seen that data may be passed into a function by way of parameter variables. Data may also be returned from a function, back to the statement that called it.

Functions that return a value are appropriately known as value-returning functions. The pow function, which you have already seen, is an example of a value-returning function. NOTE: It is possible to return multiple values from a function, but they must be "packaged" in such a way that they are treated as a single value.

Local and Global Variables with the Same Name You cannot have two local variables with the same name in the same function. This applies to parameter variables as well. Program 6.20

However, you can have a local variable or a parameter variable with the same name as a global variable, or a global constant. When you do, the name of the local or parameter variable shadows the name of the global variable or global constant. This means that the global variable or constant's name is hidden by the name of the local or parameter variable

A driver is a program that tests a function by simply calling it. If the function accepts arguments, the driver passes test data.

If the function returns a value, the driver displays the return value on the screen. This allows you to see how the function performs in isolation from the rest of the program it will eventually be part of. 6.30

Functions are ideal for use in menu-driven programs. When the user selects an item from a menu, the program can call the appropriate function. Program 6.10

In Chapters 4 and 5 you saw a menu-driven program that calculates the charges for a health club membership.

A stub is a dummy function that is called instead of the actual function it represents.

It usually displays a test message acknowledging that it was called, and nothing more. As you can see, by replacing an actual function with a stub, you can concentrate your testing efforts on the parts of the program that call the function. Primarily, the stub allows you to determine whether your program is calling a function when you expect it to, and to confirm that valid values are being passed to the function. If the stub represents a function that returns a value, then the stub should return a test value. This helps you confirm that the return value is being handled properly. When the parts of the program that call a function are debugged to your satisfaction, you can move on to testing and debugging the actual functions themselves

The function prototype must list the data type of each parameter.

Like all variables, parameters have a scope. The scope of a parameter is limited to the body of the function that uses it.

#include <iostream> using namespace std; void displayMessage() { cout << "Hello from the function displayMessage.\n"; } int main() { cout << "Hello from main.\n"; for (int count = 0; count < 5; count++) displayMessage(); // Call displayMessage cout << "Back in function main again.\n"; return 0; }

Program Output Hello from main. Hello from the function displayMessage. Hello from the function displayMessage. Hello from the function displayMessage. Hello from the function displayMessage. Hello from the function displayMessage. Back in function main again. It is possible to have many functions and function calls in a program.

A reference variable is an alias for another variable. Any changes made to the reference variable are actually performed on the variable for which it is an alias. By using a reference variable as a parameter, a function may change a variable that is defined in another function. Program 6.25

Reference variables are defined like regular variables, except you place an ampersand (&) in front of the name. For example, the following function definition makes the parameter refVar a reference variable: void doubleNum(int &refVar) { refVar *= 2; } Program 6.26

Stubs and drivers are very helpful tools for testing and debugging programs that use functions.

They allow you to test the individual functions in a program, in isolation from the parts of the program that call the functions.

Another reason to write functions is that they simplify programs. If a specific task is performed in several places in a program, a function can be written once to perform that task, and then be executed anytime it is needed.

This benefit of using functions is known as code reuse because you are writing the code to perform a task once and then reusing it each time you need to perform the task.

Static Local Variable If a function is called more than once in a program, the values stored in the function's local variables do not persist between function calls.

This is because the local variables are destroyed when the function terminates and are then recreated when the function starts again. This is shown in Program 6-21

Defining a Value-Returning Function When you are writing a value-returning function, you must decide what type of value the function will return.

This is because you must specify the data type of the return value in the function header, and in the function prototype. Recall that a void function, which does not return a value, uses the key word void as its return type in the function header.

The scope of a global variable is the portion of the program from the variable definition to the end. Program 6.17

This means that a global variable can be accessed by all functions that are defined after the global variable is defined

NOTE: If a function does not have a prototype, default arguments may be specified in the function header. The showArea function could be defined as follows: void showArea(double length = 20.0, double width = 10.0) { double area = length * width; cout << "The area is " << area << endl; }

WARNING! A function's default arguments should be assigned in the earliest occurrence of the function name. This will usually be the function prototype.

A function is executed when it is called. Function main is called automatically when a program starts, but all other functions must be executed by unction call statements.

When a function is called, the program branches to that function and executes the statements in its body.

When calling this function, arguments must always be specified for the first two parameters ( empNum and payRate ) since they have no default arguments. Here are examples of valid calls: calcPay(769, 15.75); // Use default arg for 40 hours calcPay(142, 12.00, 20); // Specify number of hours

When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined last. In the calcPay function, hours could not have been defined before either of the other parameters. The following prototypes are illegal: // Illegal prototype void calcPay(int empNum, double hours = 40.0, double payRate); // Illegal prototype void calcPay(double hours = 40.0, int empNum, double payRate);

Although C++'s default arguments are very convenient, they are not totally flexible in their use.

When an argument is left out of a function call, all arguments that come after it must be left out as well. In the displayStars function in Program 6-24 , it is not possible to omit the argument for cols without also omitting the argument for rows . For example, the following function call would be illegal: displayStars(, 3); // Illegal function call.

A function call is a statement that causes a function to execute. A function definition contains the statements that make up the function. int main () { cout << "Hello World\n"; return 0; } int -- return type main --- function name () --- parameter list cout << "... --- Function body

When creating a function, you must write its definition. All function definitions have the following parts: Return type: A function can send a value to the part of the program that executed it. The return type is the data type of the value that is sent from the function. Name: You should give each function a descriptive name. In general, the same rules that apply to variable names also apply to function names. Parameter list: The program can send data into a function. The parameter list is a list of variables that hold the values being passed to the function. Body: The body of a function is the set of statements that perform the function's operation. They are enclosed in a set of braces.

The return statement causes a function to end immediately. Program 6-11 .

When the last statement in a void function has finished executing, the function terminates and the program returns to the statement following the function call. It's possible, however, to force a function to return before the last statement has been executed. When the return statement is encountered, the function immediately terminates and control of the program returns to the statement that called the function.

Although this approach might make a program easier to create, it usually causes problems later. The reasons are as follows:

• Global variables make debugging difficult. Any statement in a program can change the value of a global variable. If you find that the wrong value is being stored in a global variable, you have to track down every statement that accesses it to determine where the bad value is coming from. In a program with thousands of lines of code, this can be difficult. • Functions that use global variables are usually dependent on those variables. If you want to use such a function in a different program, most likely you will have to redesign it so it does not rely on the global variable. • Global variables make a program hard to understand. A global variable can be modified by any statement in the program. If you are to understand any part of the program that uses a global variable, you have to be aware of all the other parts of the program that access the global variable. Because of this, you should not use global variables for the conventional purposes of storing, manipulating, and retrieving data. In most cases, you should declare variables locally and pass them as arguments to the functions that need to access them.

Here is a summary of the important points about default arguments:

• The value of a default argument must be a literal value or a named constant. • When an argument is left out of a function call (because it has a default value), all the arguments that come after it must be left out too. • When a function has a mixture of parameters both with and without default arguments, the parameters with default arguments must be declared last.

A program may be broken up into manageable functions. A function is a collection of statements that performs a specific task. Functions are commonly used to break a problem down into small manageable pieces.

So far you have experienced functions in two ways: (1) you have created a function named main in every program you've written, and (2) you have used library functions such as pow and strcmp . In this chapter you will learn how to create your own functions that can be used like library functions.

#include <iostream> using namespace std; void displayMessage() { cout << "Hello from the function displayMessage.\n"; } int main() { cout << "Hello from main.\n"; displayMessage(); cout << "Back in function main again.\n"; return 0; } Program Output Hello from main. Hello from the function displayMessage. Back in function main again. Program 6.1

The function displayMessage is called by the following statement in line 22: displayMessage(); This statement is the function call. It is simply the name of the function followed by a set of parentheses and a semicolon. Let's compare this with the function header: Function Header = void displayMessage() Function Call = displayMessage(); The function displayMessage is called by the following statement in line 22: displayMessage(); This statement is the function call. It is simply the name of the function followed by a set of parentheses and a semicolon. Let's compare this with the function header: Function Header void displayMessage() Function Call displayMessage();

Overloading Functions. Two or more functions may have the same name, as long as their parameter lists are different. Sometimes you will create two or more functions that perform the same operation, but use a different set of parameters or parameters of different data types. For instance, in Program 6-13there is a square function that uses a double parameter. But, suppose you also wanted a square function that works exclusively with integers, accepting an int as its argument. Both functions would do the same thing: return the square of their argument.

The only difference is the data type involved in the operation. If you were to use both these functions in the same program, you could assign a unique name to each function. For example, the function that squares an int might be named squareInt, and the one that squares a double might be named squareDouble. C++, however, allows you to overload function names. That means you may assign the same name to multiple functions, as long as their parameter lists are different. Program 6-27uses two overloaded square functions.

In C++, each function has a signature. The function signature is the name of the function and the data types of the function's parameters in the proper order.

The square functions in Program 6-27would have the following signatures: square(int) square(double)

NOTE: In this text, the values that are passed into a function are called arguments, and the variables that receive those values are called parameters.

There are several variations of these terms in use. Some call the arguments actual parameters and call the parameters formal parameters . Others use the terms actual argument and formal argument . Regardless of which set of terms you use, it is important to be consistent. Program 6.6

Instead of writing one long function that contains all of the statements necessary to solve a problem, several small functions that each solve a specific part of the problem can be written.

These small functions can then be executed in the desired order to solve the problem. This approach is sometimes called divide and conquer because a large problem is divided into several smaller problems that are easily solved


Conjuntos de estudio relacionados

Chapter 8 Ethical, Social, and Political Issues M/C

View Set

Abeka 5th Grade, Science Quiz 15 (6.1 - 6.3)

View Set