Chapter Questions
Why do local variables lose their values between calls to the function in which they are defined?
What is the difference between an argument and a parameter variable?
Where do you define parameter variables?
If you are writing a function that accepts an argument and you want to make sure the function cannot change the value of the argument, what do you do?
When a function accepts multiple arguments, does it matter in what order the arguments are passed?
How do you return a value from a function?
What is the advantage of breaking your application's code into several small procedures?
How would a static local variable be useful?
Give an example where passing an argument by reference would be useful.
The _________ is the part of a function definition that shows the function name, return type, and parameter list.
If a function doesn’t return a value, the word _________ will appear as its return type.
Either a function’s _________ or its _________ must precede all calls to the function.
Values that are sent into a function are called _________.
Special variables that hold copies of function arguments are called _________.
When only a copy of an argument is passed to a function, it is said to be passed by _________.
A(n) _________ eliminates the need to place a function definition before all calls to the function.
A(n) _________ variable is defined inside a function and is not accessible outside the function.
_________ variables are defined outside all functions and are accessible to any function within their scope.
_________ variables provide an easy way to share large amounts of data among all the functions in a program.
Unless you explicitly initialize global variables, they are automatically initialized to _________.
If a function has a local variable with the same name as a global variable, only the _________ variable can be seen by the function.
_________ local variables retain their value between function calls.
The _________ statement causes a function to end immediately.
_________ arguments are passed to parameters automatically if no argument is provided in the function call.
When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined _________.
The value of a default argument must be a(n) _________.
When used as parameters, _________ variables allow a function to access the parameter’s original argument.
Reference variables are defined like regular variables, except there is a(n) _________ in front of the name.
Reference variables allow arguments to be passed by ____________.
The _________ function causes a program to terminate.
Two or more functions may have the same name, as long as their _________ are different.
Examine the following function header, then write an example call to the function:void showValue(int quantity)
The following statement calls a function named half. The half function returns a value that is half that of the argument. Write the function.result = half(number);
A program contains the following function:int cube(int num){return num * num * num;}Write a statement that passes the value 4 to this function and assigns its return valueto the variable result.
Write a function named timesTen that accepts an argument. When the function is called, it should display the product of its argument multiplied by 10.
A program contains the following function:void display(int arg1, double arg2, char arg3){cout << "Here are the values: "<< arg1 << " " << arg2 << " "<< arg3 << endl;}Write a statement that calls the procedure and passes the following variables to it:int age;double income;char initial;
Write a function named getNumber that uses a reference parameter variable to accept an integer argument. The function should prompt the user to enter a number in the range of 1 through 100. The input should be validated and stored in the parameter variable.
True or FalseT F Functions should be given names that reflect their purpose.
True or FalseT F Function headers are terminated with a semicolon.
True or FalseT F Function prototypes are terminated with a semicolon.
True or FalseT F If other functions are defined before main, the program still starts executing at function main.
True or FalseT F When a function terminates, it always branches back to main, regardless of where it was called from.
True or FalseT F Arguments are passed to the function parameters in the order they appear in the function call.
True or FalseT F The scope of a parameter is limited to the function that uses it.
True or FalseT F Changes to a function parameter always affect the original argument as well.
True or FalseT F In a function prototype, the names of the parameter variables may be left out.
True or FalseT F Many functions may have local variables with the same name.
True or FalseT F Overuse of global variables can lead to problems.
True or FalseT F Static local variables are not destroyed when a function returns.
True or FalseT F All static local variables are initialized to −1 by default.
True or FalseT F Initialization of static local variables only happens once, regardless of how many times the function in which they are defined is called.
True or FalseT F When a function with default arguments is called and an argument is left out, all arguments that come after it must be left out as well.
True or FalseT F It is not possible for a function to have some parameters with default arguments and some without.
True or FalseT F The exit function can only be called from main.
True or FalseT F A stub is a dummy function that is called instead of the actual function it represents.
Each of the following functions has errors. Locate as many errors as you can.
void total(int value1, value2, value3){return value1 + value2 + value3;}
double average(int value1, int value2, int value3){double average;average = value1 + value2 + value3 / 3;}
void area(int length = 30, int width){return length * width;}
void getValue(int value&){cout << "Enter a value: ";cin >> value&;}
(Overloaded functions)int getValue(){int inputValue;cout << "Enter an integer: ";cin >> inputValue;return inputValue;}double getValue(){double inputValue;cout << "Enter a floating-point number: ";cin >> inputValue;return inputValue;}