ch6 Flashcards

1
Q

CP
16.1 Is the following a function header or a function call?
calcTotal( );

A

6.1 Function call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
6.2 Is the following a function header or a function call?
void showResults( )
A

6.2 Function header

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
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
A

6.3 I saw Elba

Able was I

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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;
}
A

6.4 void qualify()
{
cout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
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);
A

6.5 Header
Prototype
Function call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.)

A

6.6 void timesTen(int number)
{
cout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

6.7 Write a function prototype for the timesTen function you wrote in Question 6.6.

A

6.7 void timesTen(int);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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
A
6.8 
0     0
1      2
2     4
3     6
4     8
5     10
6     12
7      14
8     16
9     18
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
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
A
6.9 
0 1.5
1.5 0
0 10
0 1.5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

6.10 void showDollars(double amount)
{
cout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

6.11 How many return values may a function have?

A

6.11 One

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

6.12 Write a header for a function named distance. The function should return a
double and have two double parameters: rate and time.

A

6.12 double distance(double rate, double time)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

6.13 int days(int years, int months, int weeks)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

6.14 Write a header for a function named getKey. The function should return a char
and use no parameters.

A
6.14 
char getKey( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

6.15 Write a header for a function named lightYears. The function should return a
long and have one long parameter: miles.

A

6.15 long lightYears(long miles)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

6.16 What is the difference between a static local variable and a global variable

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
6.17 What is the output of the following program?
#include 
using namespace std;
void myFunc(); // Function prototype
int main()
{
int var = 100;
cout
A

6.17 100
50
100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
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
A
6.18 
10
11
12
13
14
15
16
17
18
19
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

6.19 What kinds of values may be specified as default arguments?

A

6.19 Literals or constants

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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.

A
6.20 Prototype:
void compute(double, int = 5, long = 65536);
Header:
void compute(double x, int y, long z)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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.

A
6.21 Prototype:
void calculate(long, double&, int = 47);
Header:
void calculate(long x, double &y, int z)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
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
A
6.22 
5 10 15
9 10 15
6 15 15
4 11 16
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
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;
}
A
6.23 
0 00
Enter two numbers: 12 14
12 140
14 15-1
14 15-1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
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
A
  1. 24

1. 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
``` 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
26
1. Why do local variables lose their values between calls to the function in which they are defined?
1. Because they are created in memory when the function begins execution, and are destroyed when the function ends.
27
2. 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
28
3. Where do you define parameter variables?
3. Inside the parentheses of a function header.
29
``` 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
30
5. 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. ```
31
6. How do you return a value from a function?
write the keyword return followed by the value you want to return
32
7. What is the advantage of breaking your application s code into several small procedures
7. 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.
33
8. 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
34
9. 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; } ```
35
10. The _________ is the part of a function definition that shows the function name, return type, and parameter list.
function header
36
11. If a function doesn't return a value, the word _________ will appear as its return type.
11. void
37
12. Either a function s _________ or its _________ must precede all calls to the function.
function's definition / prototyope
38
13. Values that are sent into a function are called _________.
13. arguments
39
14. Special variables that hold copies of function arguments are called _________.
parameters
40
15. When only a copy of an argument is passed to a function, it is said to be passed by _________.
15. value
41
16. A(n) _________ eliminates the need to place a function definition before all calls to the function.
a function prototype
42
17. A(n) _________ variable is defined inside a function and is not accessible outside the function.
17. local
43
18. _________ variables are defined outside all functions and are accessible to any function within their scope.
global
44
19. _________ variables provide an easy way to share large amounts of data among all the functions in a program.
19. global
45
20. Unless you explicitly initialize global variables, they are automatically initialized to _________.
zero
46
21. If a function has a local variable with the same name as a global variable, only the _________ variable can be seen by the function.
21. local
47
22. _________ local variables retain their value between function calls.
static
48
23. The _________ statement causes a function to end immediately.
23. return
49
24. _________ arguments are passed to parameters automatically if no argument is provided in the function call.
default
50
25. When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined _________.
25. last
51
26. The value of a default argument must be a(n) _________.
a literal value or a named constant
52
27. When used as parameters, _________ variables allow a function to access the parameter s original argument.
27. reference
53
28. Reference variables are defined like regular variables, except there is a(n) _________ in front of the name.
ampersand(&)
54
29. Reference variables allow arguments to be passed by ____________.
29. reference
55
30. The _________ function causes a program to terminate.
exit( )
56
31. Two or more functions may have the same name, as long as their _________ are different.
31. parameter lists
57
``` 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);
58
33. 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);
33. double half(double num) { return num / 2; }
59
``` 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
60
35. 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.
35. void timesTen(int num) { cout << (num * 10) << endl; }
61
``` 36. A program contains the following function. void display(int arg1, double arg2, char arg3) { cout ```
display (age, income,initial); | is the functional call
62
37. 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; } } ```
63
True or False | 38. T F Functions should be given names that reflect their purpose.
t
64
39. T F Function headers are terminated with a semicolon.
f
65
40. T F Function prototypes are terminated with a semicolon.
t
66
41. T F If other functions are defined before main, the program still starts executing at function main.
t
67
42. 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
68
43. T F Arguments are passed to the function parameters in the order they appear in the function call.
t
69
44. T F The scope of a parameter is limited to the function which uses it.
t
70
45. T F Changes to a function parameter always affect the original argument as well.
f
71
46. T F In a function prototype, the names of the parameter variables may be left out.
t
72
47. T F Many functions may have local variables with the same name.
t
73
48. T F Overuse of global variables can lead to problems.
t
74
49. T F Static local variables are not destroyed when a function returns.
t
75
50. T F All static local variables are initialized to 1 by default.
f. an static variable is initialized and starts with zero
76
51. 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
77
52. 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
78
53. T F It is not possible for a function to have some parameters with default arguments and some without.
f
79
54. 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
80
55. T F A stub is a dummy function that is called instead of the actual function it represents.
t
81
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; } ```
82
``` 57. double average(int value1, int value2, int value3) { double average; average = value1 + value2 + value3 / 3; } ```
57. The assignment statement should read: average = (value1 + value2 + value3) / 3.0; The function is defined as a double but returns no value.
83
58. void area(int length = 30, int width) { return length * width; }
``` correct: int area ( int length = 30, int width) { return length*width; } ```
84
``` 59. void getValue(int value&) { cout << "Enter a value: "; cin >> value&; } ```
59. The parameter should be defined as: int &value The cin statement should read: cin >> value;
85
``` 60. (Overloaded functions) int getValue() { int inputValue; cout > inputValue; return inputValue; } double getValue() { double inputValue; cout > inputValue; return inputValue; } ```
see