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