ch6 Flashcards
CP
16.1 Is the following a function header or a function call?
calcTotal( );
6.1 Function call
6.2 Is the following a function header or a function call? void showResults( )
6.2 Function header
6.3 What will the output of the following program be if the user enters 10? #include using namespace std; void func1( ) { cout > input; if (input
6.3 I saw Elba
Able was I
6.4 The following program skeleton determines whether a person qualifies for a
credit card. To qualify, the person must have worked on his or her current job for
at least two years and make at least $17,000 per year. Finish the program by writing
the definitions of the functions qualify and noQualify. The function
qualify should explain that the applicant qualifies for the card and that the
annual interest rate is 12%. The function noQualify should explain that the
applicant does not qualify for the card and give a general explanation why.
#include
using namespace std;
// You must write definitions for the two functions qualify
// and noQualify.
int main() { double salary; int years; cout > salary; cout > years; if (salary >= 17000.0 && years >= 2) qualify(); else noQualify(); return 0; }
6.4 void qualify()
{
cout
6.5 Indicate which of the following is the function prototype, the function header, and the function call: void showNum(double num) void showNum(double); showNum(45.67);
6.5 Header
Prototype
Function call
6.6 Write a function named timesTen. The function should have an integer parameter
named number. When timesTen is called, it should display the product of number
times ten. (Note: just write the function. Do not write a complete program.)
6.6 void timesTen(int number)
{
cout
6.7 Write a function prototype for the timesTen function you wrote in Question 6.6.
6.7 void timesTen(int);
6.8 What is the output of the following program? #include using namespace std; void showDouble(int); // Function prototype int main() { int num; for (num = 0; num
6.8 0 0 1 2 2 4 3 6 4 8 5 10 6 12 7 14 8 16 9 18
6.9 What is the output of the following program? #include using namespace std; void func1(double, int); // Function prototype int main() { int x = 0; double y = 1.5; cout
6.9 0 1.5 1.5 0 0 10 0 1.5
6.10 The following program skeleton asks for the number of hours you’ve worked and
your hourly pay rate. It then calculates and displays your wages. The function
showDollars, which you are to write, formats the output of the wages.
#include
using namespace std;
void showDollars(double); // Function prototype
int main()
{
double payRate, hoursWorked, wages;
cout > hoursWorked;
cout > payRate;
wages = hoursWorked * payRate;
showDollars(wages);
return 0;
}
// You must write the definition of the function showDollars
// here. It should take one parameter of the type double.
// The function should display the message “Your wages are $”
// followed by the value of the parameter. It should be displayed
// with 2 places of precision after the decimal point, in fixed
// notation, and the decimal point should always display.
6.10 void showDollars(double amount)
{
cout
6.11 How many return values may a function have?
6.11 One
6.12 Write a header for a function named distance. The function should return a
double and have two double parameters: rate and time.
6.12 double distance(double rate, double time)
6.13 Write a header for a function named days. The function should return an int
and have three int parameters: years, months, and weeks.
6.13 int days(int years, int months, int weeks)
6.14 Write a header for a function named getKey. The function should return a char
and use no parameters.
6.14 char getKey( )
6.15 Write a header for a function named lightYears. The function should return a
long and have one long parameter: miles.
6.15 long lightYears(long miles)
6.16 What is the difference between a static local variable and a global variable
6.16 A static local variable’s scope is limited to the function in which it is declared. A global
variable’s scope is the portion of the program beginning at its declaration to the end.
6.17 What is the output of the following program? #include using namespace std; void myFunc(); // Function prototype int main() { int var = 100; cout
6.17 100
50
100
6.18 What is the output of the following program? #include using namespace std; void showVar(); // Function prototype int main() { for (int count = 0; count
6.18 10 11 12 13 14 15 16 17 18 19
6.19 What kinds of values may be specified as default arguments?
6.19 Literals or constants
6.20 Write the prototype and header for a function called compute. The function
should have three parameters: an int, a double, and a long (not necessarily in
that order). The int parameter should have a default argument of 5, and the
long parameter should have a default argument of 65536. The double parameter
should not have a default argument.
6.20 Prototype: void compute(double, int = 5, long = 65536); Header: void compute(double x, int y, long z)
6.21 Write the prototype and header for a function called calculate. The function
should have three parameters: an int, a reference to a double, and a long (not
necessarily in that order.) Only the int parameter should have a default argument,
which is 47.
6.21 Prototype: void calculate(long, double&, int = 47); Header: void calculate(long x, double &y, int z)
6.22 What is the output of the following program? #include using namespace std; void test(int = 2, int = 4, int = 6); int main() { test(); test(6); test(3, 9); test(1, 5, 7); return 0; } void test (int first, int second, int third) { first += 3; second += 6; third += 9; cout
6.22 5 10 15 9 10 15 6 15 15 4 11 16
6.23 The following program asks the user to enter two numbers. What is the output of the program if the user enters 12 and 14? #include using namespace std; void func1(int &, int &); void func2(int &, int &, int &); void func3(int, int, int); int main() { int x = 0, y = 0, z = 0; cout > a >> b; } void func2(int &a, int &b, int &c) { b++; c--; a = b + c; } void func3(int a, int b, int c) { a = b - c; }
6.23 0 00 Enter two numbers: 12 14 12 140 14 15-1 14 15-1
6.24 What is the output of the following program? #include #include using namespace std; void showVals(double, double); int main() { double x = 1.2, y = 4.5; showVals(x, y); return 0; } void showVals(double p1, double p2) { cout
- 24
1. 2
6.25 What is the output of the following program? #include using namespace std; int manip(int); int manip(int, int); int manip(int, double); int main() { int x = 2, y= 4, z; double a = 3.1; z = manip(x) + manip(x, y) + manip(y, a); cout
6.25 30
- Why do local variables lose their values between calls to the function in which they
are defined?
- Because they are created in memory when the function begins execution, and are destroyed
when the function ends.
- What is the difference between an argument and a parameter variable?
argument variable is a variable which is used to send a value to the functions, should not write data type before the argument variable.
parameter variable is used to accept a value into the functions. write data type before it
- Where do you define parameter variables?
- Inside the parentheses of a function header.
4. 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?
pass by value
- When a function accepts multiple arguments, does it matter in what order the arguments
are passed in?
5. Yes. The first argument is passed into the parameter variable that appears first inside the function header’s parentheses. Likewise, the second argument is passed into the second parameter, and so on.
- How do you return a value from a function?
write the keyword return followed by the value you want to return
- What is the advantage of breaking your application s code into several small procedures
- It makes the program easier to manage. Imagine a book that has a thousand pages, but isn’t divided into chapters or sections. Trying to find a single topic in the book would be very difficult.
Real-world programs can easily have thousands of lines of code, and unless they are modularized, they can be very difficult to modify and maintain.
- How would a static local variable be useful?
static variables are not destroyed when a function returns.they exist for the lifetime of the program even though their scope is only in function
- Give an example where passing an argument by reference would be useful.
9. A function such as the following could be written to get user input. The input is stored in the variables that are passed as arguments. void getValues(int &x, int &y) { cout << "Enter a number: "; cin >> x; cout << "Enter another number: "; cin >> y; }
- The _________ is the part of a function definition that shows the function name,
return type, and parameter list.
function header
- If a function doesn’t return a value, the word _________ will appear as its return type.
- void
- Either a function s _________ or its _________ must precede all calls to the function.
function’s definition / prototyope
- Values that are sent into a function are called _________.
- arguments
- Special variables that hold copies of function arguments are called _________.
parameters
- When only a copy of an argument is passed to a function, it is said to be passed by
_________.
- value
- A(n) _________ eliminates the need to place a function definition before all calls to the
function.
a function prototype
- A(n) _________ variable is defined inside a function and is not accessible outside the
function.
- local
- _________ variables are defined outside all functions and are accessible to any function
within their scope.
global
- _________ variables provide an easy way to share large amounts of data among all
the functions in a program.
- global
- Unless you explicitly initialize global variables, they are automatically initialized to
_________.
zero
- 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
- _________ local variables retain their value between function calls.
static
- The _________ statement causes a function to end immediately.
- return
- _________ arguments are passed to parameters automatically if no argument is
provided in the function call.
default
- When a function uses a mixture of parameters with and without default arguments,
the parameters with default arguments must be defined _________.
- last
- The value of a default argument must be a(n) _________.
a literal value or a named constant
- When used as parameters, _________ variables allow a function to access the
parameter s original argument.
- reference
- Reference variables are defined like regular variables, except there is a(n) _________ in
front of the name.
ampersand(&)
- Reference variables allow arguments to be passed by ____________.
- reference
- The _________ function causes a program to terminate.
exit( )
- Two or more functions may have the same name, as long as their _________ are
different.
- parameter lists
32. Examine the following function header, then write an example call to the function. void showValue(int quantity)
Function call
int quantity = 5;
showValue (quantity);
or
showValue(16);
- 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);
- double half(double num)
{
return num / 2;
}
34. 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 value to the variable result.
n/a
- Write a function named timesTen that accepts an argument. When the function is
called, it should display the product of its argument multiplied times 10.
- void timesTen(int num)
{
cout «_space;(num * 10) «_space;endl;
}
36. A program contains the following function. void display(int arg1, double arg2, char arg3) { cout
display (age, income,initial);
is the functional call
- 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.
37. void getNumber(int &num) { cout > num; while (num 100) { cout > num; } }
True or False
38. T F Functions should be given names that reflect their purpose.
t
- T F Function headers are terminated with a semicolon.
f
- T F Function prototypes are terminated with a semicolon.
t
- T F If other functions are defined before main, the program still starts executing
at function main.
t
- T F When a function terminates, it always branches back to main, regardless of
where it was called from.
f. After function termination it backs to function call where it is called
- T F Arguments are passed to the function parameters in the order they appear in
the function call.
t
- T F The scope of a parameter is limited to the function which uses it.
t
- T F Changes to a function parameter always affect the original argument as well.
f
- T F In a function prototype, the names of the parameter variables may be left out.
t
- T F Many functions may have local variables with the same name.
t
- T F Overuse of global variables can lead to problems.
t
- T F Static local variables are not destroyed when a function returns.
t
- T F All static local variables are initialized to 1 by default.
f. an static variable is initialized and starts with zero
- T F Initialization of static local variables only happens once, regardless of how
many times the function in which they are defined is called.
t
- T 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.
t
- T F It is not possible for a function to have some parameters with default arguments
and some without.
f
- T F The exit function can only be called from main.
f, as exit function csuses program to terminate regardless of which function or control mechanism executing
- T F A stub is a dummy function that is called instead of the actual function it
represents.
t
Find the Errors
Each of the following functions has errors. Locate as many errors as you can.
56. void total(int value1, value2, value3)
{
return value1 + value2 + value3;
}
corrected: int total( int value1, int value2, int value3) { return value1+value2+value3; }
57. double average(int value1, int value2, int value3) { double average; average = value1 + value2 + value3 / 3; }
- The assignment statement should read:
average = (value1 + value2 + value3) / 3.0;
The function is defined as a double but returns no value.
- void area(int length = 30, int width)
{
return length * width;
}
correct: int area ( int length = 30, int width) { return length*width; }
59. void getValue(int value&) { cout << "Enter a value: "; cin >> value&; }
- The parameter should be defined as:
int &value
The cin statement should read:
cin»_space; value;
60. (Overloaded functions) int getValue() { int inputValue; cout > inputValue; return inputValue; } double getValue() { double inputValue; cout > inputValue; return inputValue; }
see