Saturday, 11 January 2014

CHAPTER 5—FUNCTIONS

CHAPTER 5—FUNCTIONS 


Questions:
----------
1. 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.
2. A function itself is called the function d_________.
3. Write a function called foo() that displays the word foo.
4. A one-statement description of a function is referred to as a function d_________ or a p_________.
5. The statements that carry out the work of the function constitute the function _________.
6. A program statement that invokes a function is a function _________.
7. The first line of a function definition is referred to as the _________.
8. 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.
9. True or false: When arguments are passed by value, the function works with the original arguments in the calling program.
10. What is the purpose of using argument names in a function declaration?
11. Which of the following can legitimately be passed to a function?
a. A constant
b. A variable
c. A structure
d. A header file
12. What is the significance of empty parentheses in a function declaration?
13. How many values can be returned from a function?
14. 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.
15. Where is a function’s return type specified?
16. A function that doesn’t return anything has return type _________.
17. Here’s a function:
int times2(int a)
{
return (a*2);
}



Write a main() program that includes everything necessary to call this function.
18. 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.
19. What is a principle reason for passing arguments by reference?
20. 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.
21. 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.
22. In general, an inline function executes _________ than a normal function, but requires _________ memory.
23. Write the declarator for an inline function named foobar() that takes one argument of type float and returns type float.
24. 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.
25. 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.
26. Storage class is concerned with the _________ and _________ of a variable.
27. What functions can access an external variable that appears in the same file with them?
28. What functions can access an automatic variable?
29. A static automatic 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.
30. In what unusual place can you use a function call when a function returns a value by reference?
__________
My answers:
-----------
1. reduce program size, help organize a program into conceptual units.
2. definition.
3. void foo(void) {cout<<"foo"; }
4. declairation, property value.
5. work.
6. caller.
7. function name.
8. a variable in the function that receives a value from the calling program, a value sent to the function by the calling program.
9. False.
10. Facilite knowing the concept of argument that I'll pass to the function.
11. A constant, A variable, A structure.
12. void.
13. one.
14. True.
15. before it's name.
16. void.
17. void main(void) {int a, time_two=times2(a);}
18. the function accesses the argument’s original value in the calling program.
19. Use the original variable, not copy large values & return many different parammeters.
20. are a group of functions with the same name, make life simpler for programmers.
21. int bar(char); int bar(char, char);
22. faster, more.
23. inline float foobar(float);
24. may be supplied by the calling program, may be supplied by the function.
25. char blyth(int, float=3.14159);
26. ???, ???.
27. All defined function.
28. Functions that contains in their variables definition the "auto" keyword.
29. retain a value when a function is not executing.
30. As assigned value to it.
___________
Answers:
--------
1. d (half credit for b)
2. definition
3.
void foo()
{
cout << “foo”;
}

4. declaration, prototype
5. body
6. call
7. declarator
8. c
9. False
10. To clarify the purpose of the arguments.
11. a, b, c
12. Empty parentheses mean the function takes no arguments.
13. one
14. True
15. At the beginning of the declaration and declarator.
16. void
17.
main()
{
int times2(int); // prototype
int alpha = times2(37); // function call
}

18. d
19. To modify the original argument (or to avoid copying a large argument).
20. a, c
21.
int bar(char);
int bar(char, char);

22. faster, more
23. inline float foobar(float fvar)
24. a, b
25. char blyth(int, float=3.14159);
26. visibility, lifetime
27. Those functions defined following the variable definition.
28. The function in which it is defined.
29. b, d
30. On the left side of the equal sign.
________

No comments:

Post a Comment