Chapter 7
FUNCTIONS
QUESTIONS
True/False
1. It is possible to supply different actual parameter names every time a function is called.
2. Using pass-by-value, a variable name cannot be used as an actual parameter.
3. If a C++ function does not use parameters, you still must put parentheses around the empty parameter list.
4. Using pass-by-reference, passing an
int actual parameter to a float formal parameter is acceptable to the compiler but may produce incorrect results.5. If a module consists of only a single line, it is usually best to code it directly into the program rather than turn it into a function.
6. You must be careful when you choose to code certain modules as functions because the correct functioning of the program may be affected.
7. In C++, corresponding formal and actual parameters must have the same name.
8. In C++, a function definition may have another function definition nested within it.
9. With parameter passage by reference, the address of the caller's actual parameter is sent to the function.
10. If there are several items in a parameter list, the compiler matches the formal and actual parameters by their relative positions in the formal and actual parameter lists.
11. The use of reference parameters helps to avoid unintentional changes to actual parameters.
12. If a function contains a statement that changes a value parameter, only the copy of the actual parameter is changed, not the original.
13. A single function heading can declare both reference and value parameters.
14. Any parameter that can be classified as both incoming and outgoing must be coded as a reference parameter.
15. When a parameter is passed by reference, the actual parameter can be any expression.
16. In C++, a function can be declared several times but can be defined only once.
Multiple Choice
17. Which of the following is not a reason why programmers write their own functions?
a. to help organize and clarify programs
b. to make programs execute faster than they would with sequential flow of control
c. to allow the reuse of the same code (function) within the same program
d. to allow the reuse of the same code (function) within another program
18. When parameters are passed between the calling code and the called function, formal and actual parameters are matched by:
a. their data types
b. their relative positions in the formal and actual parameter lists
c. their names
d. whether they are inputs to or outputs from the function
19. Parameter passage by value is used if a parameter's data flow is:
a. one-way, into the function.
b. one-way, out of the function.
c. two-way, into and out of the function.
d. a and b above
e. b and c above
20. Parameter passage by reference is used if a parameter's data flow is:
a. one-way, into the function.
b. one-way, out of the function.
c. two-way, into and out of the function.
d. a and b above
e. b and c above
21. In C++, a function prototype is:
a. a declaration but not a definition.
b. a definition but not a declaration.
c. both a declaration and a definition.
d. neither a declaration nor a definition.
22. Given the function prototype
void Fix( int&, float );
which of the following is an appropriate function call?
(someInt is of type int, and someFloat is of type float.)a. Fix(24, 6.85);
b.
someFloat = 0.3 * Fix(someInt, 6.85);c. Fix(someInt + 5, someFloat);
d. a and c above
e. none of the above
23. Which of the following is the correct function heading for a parameterless function named
PrintStars?a. void PrintStars
b. void PrintStars;
c. void PrintStars()
d.
void PrintStars();e. void PrintStars( int n )
24. Given the function heading
void GetNums( int howMany,
float& alpha,
float& beta )
which of the following is a valid function prototype for
GetNums?a.
void GetNums( int howMany, float& alpha, float& beta );b.
void GetNums( int, float&, float& );c.
int GetNums( int, float&, float& );
d.
a and b abovee.
a, b, and c above25. A function
SomeFunc has two formal parameters, alpha and beta, of type int. The data flow for alpha is one-way, into the function. The data flow for beta is two-way, into and out of the function. What is the most appropriate function heading for SomeFunc?a. void SomeFunc( int alpha, int beta )
b. void SomeFunc( int& alpha, int beta )
c.
void SomeFunc( int alpha, int& beta )
d.
void SomeFunc( int& alpha, int& beta )26. Given the function definition
void Twist( int a,
int& b )
{
int c;
c = a + 2;
a = a * 3;
b = c + a;
}
what is the output of the following code fragment that invokes
Twist? (All variables are of type int.)r = 1;
s = 2;
t = 3;
Twist(t, s);
cout << r << ' ' << s << ' ' << t << endl;
a. 1 14 3
b.
1 10 3c.
5 14 3d.
1 14 9e. none of the above
27. Which of the following statements about value parameters is true?
a. The actual parameter is never modified by execution of the called function.
b. The formal parameter is never modified by execution of the called function.
c. The actual parameter must be a variable.
d. The actual parameter cannot have a Boolean value.
e. b and c above
28. Which of the following statements about reference parameters is true?
a. The actual parameter can be modified by execution of the called function.
b. The formal parameter can be modified by execution of the called function.
c. The actual parameter cannot be a variable.
d. The actual parameter cannot have an integer value.
e. a and b above
29. Consider the function definition
void Demo( int intVal,
sfloat& floatVal )
{
intVal = intVal * 2;
floatVal = float(intVal) + 3.5;
}
Suppose that the caller has variables
myInt and myFloat whose values are 20 and 4.8, respectively. What are the values of myInt and myFloat after return from the following function call?Demo(myInt, myFloat);
a.
myInt = 20 and myFloat = 43.5b.
myInt = 40 and myFloat = 4.8c.
myInt = 20 and myFloat = 4.8d.
myInt = 40 and myFloat = 43.5e. none of the above
30. Consider the function definition
void Demo( int& intVal,
float floatVal )
{
intVal = intVal * 2;
floatVal = float(intVal) + 3.5;
}
Suppose that the caller has variables
myInt and myFloat whose values are 20 and 4.8, respectively. What are the values of myInt and myFloat after return from the following function call?Demo(myInt, myFloat);
a.
myInt = 20 and myFloat = 43.5b.
myInt = 40 and myFloat = 4.8c.
myInt = 20 and myFloat = 4.8
d. myInt = 40 and myFloat = 43.5
e. none of the above
31. If an ampersand (
a. a constant
b. a variable name
c. an arbitrary expression
d. a and b above
e. a, b, and c above
32. For the function definition
void Func( int& gamma )
{
gamma = 245;
}
which of the following comments describes the direction of data flow for
gamma?a.
/* in */b.
/* out */c.
/* inout */33. For the function definition
void Func( int gamma )
{
cout << 3 * gamma;
}
which of the following comments describes the direction of data flow for
gamma?a.
/* in */b.
/* out */c.
/* inout */
34. For the function definition
void Func( int& gamma )
{
gamma = 3 * gamma;
}
which of the following comments describes the direction of data flow for
gamma?a.
/* in */b.
/* out */
c.
/* inout */35. Consider the function definition
void DoThis( int& alpha,
int beta )
{
int temp;
alpha = alpha + 100;
temp = beta;
beta = 999;
}
Suppose that the caller has integer variables
gamma and delta whose values are 10 and 20, respectively. What are the values of gamma and delta after return from the following function call?DoThis(gamma,delta);
a.
gamma = 10 and delta = 20b.
gamma = 110 and delta = 20c.
gamma = 10 and delta = 999d.
gamma = 110 and delta = 999e. none of the above
36. For the function definition
void Func( int& alpha,
int beta )
{
int delta;
delta = beta;
alpha = beta;
beta = 23;
cout << delta * beta;
}
what is the function precondition?
a.
// Precondition: alpha is assignedb.
// Precondition: beta is assignedc.
// Precondition: delta is assignedd.
// Precondition: alpha and beta are assignede.
// Precondition: alpha, beta, and delta are assigned37. For the function definition
void Func( int& alpha,
int beta )
{
alpha = beta;
beta = 23;
}
what is the function postcondition?
a.
// Postcondition: alpha == beta && beta == 23b.
// Postcondition: alpha<entry> == beta && beta == 23c.
// Postcondition: alpha == beta<entry>d.
// Postcondition: alpha == betaFill-In
38. A(n) ____________________ is a variable or expression listed in a call to a function.
39. A(n) ____________________ is a variable declared in a function heading.
40. A(n) ____________________ parameter is a formal parameter that receives the location (address) of the caller's actual parameter.
41. A(n) ____________________ is a variable that is declared within a block and is not accessible outside of that block.
42. When a function uses a(n) ____________________ parameter, the formal parameter receives a copy of the actual parameter's value.
43. A(n) ____________________ is a function declaration that includes the function body.
44. The pass-by-____________________ mechanism is used for a one-way flow of information out of the function.
45. The pass-by-____________________ mechanism is used for a two-way flow of information into and out of the function.
46. In C++, a(n) ____________________ is a function declaration without the body of the function.
47. A comment describing the conditions that are necessary before calling a function is known as a function ____________________.
48. The pass-by-____________________ mechanism must be used if the calling code is to receive information back from the function.
49. The pass-by-____________________ mechanism gives the called function the privilege of accessing the actual parameter.
50. The pass-by-____________________ mechanism is used for the parameter
void DoSomething( char& alpha,
char beta );
51. The pass-by-____________________ mechanism is used for the parameter
beta below:void DoSomething( char& alpha,
char beta );
52. A(n) ____________________ is an assertion used to describe the conditions that are true when a function completes its execution.
53. The textbook uses this suffix in postconditions to denote the value of the parameter at the moment the function was called: ____________________.