Chapter 5 (Functions) - Questions
Write a function called foo( ) that displays the word foo.
void foo( ) { cout << "foo"; }
What is the significance of empty parentheses in a function declaration?
Empty parentheses mean the function takes no arguments
A one-statement description of a function is referred to as a function d_________ or a p_________.
declaration, prototype
The first line of a function definition is referred to as the _________.
declarator
A function itself is called the function d_________.
definition
A program statement that invokes a function is a function _________.
call
True or false: When arguments are passed by value, the function works with the original arguments in the calling program.
false
In general, an inline function executes _________ than a normal function, but requires _________ memory.
faster, more
Here's a function: int times2(int a) { return (a*2); } Write a main() program that includes everything necessary to call this function.
main() { // prototype int times2(int); // function call int alpha = times2(37); }
What functions can access a local variable?
the function in which it is defined
What functions can access a global variable that appears in the same file with them?
those functions defined following the variable definition
What is a principal reason for passing arguments by reference?
to modify the original argument (or to avoid copying a large argument)
What is the purpose of using argument names in a function declaration?
To clarify the purpose of the arguments
A default argument has a value that a. may be supplied by the calling program. b. may be supplied by the function. c. must have a constant value. d. must have a variable value.
a, b
Which of the following can legitimately be passed to a function? a. A constant b. A variable c. A structure d. A header file
a, b, c
Overloaded functions a. are a group of functions with the same name. b. all have the same number and types of arguments. c. make life simpler for programmers. d. may fail unexpectedly due to stress.
a, c
Where is a function's return type specified?
at the beginning of the declaration and declarator
A static local variable is used to a. make a variable visible to several functions. b. make a variable visible to only one function. c. conserve memory when a function is not executing. d. retain a value when a function is not executing.
b, d
The statements that carry out the work of the function constitute the function _________.
body
A function argument is a. a variable in the function that receives a value from the calling program. b. a way that functions resist accepting the calling program's values. c. a value sent to the function by the calling program. d. a value returned by the function to the calling program.
c
Write a declaration for a function called blyth() that takes two arguments and returns type char. The first argument is type int, and the second is type float with a default value of 3.14159.
char blyth(int, float=3.14159);
When an argument is passed by reference a. a variable is created in the function to hold the argument's value. b. the function cannot access the argument's value. c. a temporary variable is created in the calling program to hold the argument's value. d. the function accesses the argument's original value in the calling program.
d
A function's single most important role is to a. give a name to a block of code. b. reduce program size. c. accept arguments and provide a return value. d. help organize a program into conceptual units.
d (half credit for b)
Write the declarator for an inline function named foobar() that takes one argument of type float and returns type float.
inline float foobar(float fvar)
Write declarations for two overloaded functions named bar(). They both return type int. The first takes one argument of type char, and the second takes two arguments of type char. If this is impossible, say why.
int bar(char); int bar(char, char);
In what unusual place can you use a function call when a function returns a value by reference?
on the left side of the equal sign
How many values can be returned from a function?
one
True or false: When a function returns a value, the entire function call can appear on the right side of the equal sign and be assigned to another variable.
true
Scope and storage class are concerned with the _________ and _________ of a variable.
visibility, lifetime
A function that doesn't return anything has return type _________.
void