Study Guide 6 Answers

  1. d: A function does name a block of code that can be called many times, which allows programmers to reuse the code and reduce the program's size. Nevertheless, the most important thing that functions do for a program is to help organize it into logical, manageable conceptual units or "chunks." The units help programmers think more about how logically to solve a problem than about the detailed steps needed.
  2. b and c
  3. Function definitions and concepts:
  4. Function code
  5. a, b, and c
  6. c
  7. 1
  8. void
  9. a: When a function has a return type of "void," a call to the function can't be used as an expression
  10. a, b, c, and d: When a function has a non-void return type, a call to the function can be used as an expression or as a statement
  11. b and c
  12. b, c, and d
  13. a and c: A prototype is essentially a function header followed by a semicolon. A prototype must include all typing information (return and all parameters), but the parameter names are optional. Prototypes do not have bodies. Prototypes are function declarations, which are entered into the symbol table but do not otherwise use memory.
  14. d: Function definitions must include a function body, all typing information, and all parameter names.
  15. a
  16. a and b
  17. d
  18. a: Function definitions include typing information (for the return value and each argument), and they have a body.
  19. c: Function prototypes include typing information (for the return value and each argument), but they DO NOT have a body. Variable names are also optional).
  20. b: Function calls DO NOT include typing information but must have valid arguments. In this code fragment, the "int" is part of the definition of variable z and has nothing to do with the function or its call.
  21. a: (1) The formal argument in the function definition has no additional symbols; (2) the function uses the argument in the body without additional operators; and (3) the function call does not have additional operators or symbols.
  22. b: (1) The formal argument in the function definition includes the "&" that denotes a reference; this symbol would also appear in the function's prototype. However, (2) the function uses the argument in the body without additional operators, and (3) the function call does not have additional operators or symbols.
  23. c: (1) The formal argument in the function definition includes the "*" that denotes a pointer (this symbol would also appear in the function's prototype). Additionally, (2) the function dereferences (the "*" operator) the argument whenever it uses it in the body; and (3) the actual value (&x) in the function call is an expression that results in an address or pointer.
  24. a
  25. b
  26. c
  27. a: The function implements a pass-by-value - the function has a private copy of the value, and what the function does to that value does not affect the original value. Pass-by-value sends data in only one direction: from the caller to the function. Both variables are named x, but they are different variables.
  28. b: The function implements a pass-by-reference. Variable x is an alias for variable a - that is, one variable has two names, a and x. When the function increments x, it also increments a because they are the same variable. Pass-by-reference allows data to flow in two directions: from the caller to the function, from the function to the caller, or both.
  29. b: The function implements a pass-by-pointer. Variable x points to a - so the program saves data in one location but can access it through two names. When the program increments x, it also increments a. Pass-by-pointer allows data to flow in two directions: from the caller into the function, from the function to the caller, or both.
  30. a
  31. b
  32. b
  33. b: It's impossible to differentiate between pass-by-value and pass-by-reference by looking only at the function call. But, the function returns a value through the argument list (the function changes x from 10 to 11), so the call must be by-reference or by-pointer. Pass-by-pointer requires special syntax, which is not present; therefore, x must be passed-by-reference.
  34. c: The function returns a value through the argument list (the function changes x from 10 to 11), so the call must be by-reference or by-pointer. The call uses the address-of operator, the hallmark of pass-by-pointer.
  35. b
  36. c: "a = 10" is an example of a default argument. If the user does not supply an argument in the function call (line 11), then the default value of 10 is used.
  37. d: "a = 10" is an example of a default argument. The user may override the default value by supplying an argument in the function call (line 11), which overrides the default value of 10.
  38. c: Functions can have multiple default arguments.
  39. d: It is possible to supply some values in a function call with default arguments while accepting some default values. However, working from left to right, once you accept a default value, you must accept the remaining defaults to the end of the argument list.
  40. d: Although the function has default values for all its parameters, the function call overrides them. The "100" and "25" in the function call on line 11 replace the default values "10" and "5" on line 4. So "100" is passed to a and "25" to b
  41. a: Working from left to right on line 4, once you specify a default value for one argument, you must provide default values for the remaining arguments in the list.
  42. e
  43. scope
  44. object
  45. d
  46. b: Function call pass arguments to functions by POSITION, not by name; so z in the call is passed to x in the function, and x in the call is passed to z in the function.
  47. 10
  48. 20
  49. a, d, e, and f

    (a) works but is generally discouraged. (d) and (e) are most often used if the function calculates more than one value or returns another value (e.g., a status or error code). (f) programmers generally use this technique when they can.

  50. Questions 50-52

    If the question doesn't require the function to print to the console, don't include a cout statement. And finally, remember that C++ requires an explicit * for the multiplication operator; xxx doesn't multiply x together three times, it "looks like" a variable named "xxx."

  51. int cube(int x)
    {
    	return x * x * x;
    	// return pow(x,3);	// alternate
    }
  52. void cube(int& x)
    {
    	x = x * x * x;
    	// x = pow(x,3);		// alternate
    }
  53. The asterisk (*) appears many times in the assignment statement and to fully understand the statement it is necessary to recognize that * actually represents two distinct operators: multiplication and dereference.  The dereference operator is painted red to distinguish it from the multiplication operator:

    void cube(int* x)
    {
    	*x = *x * *x * *x;
    	// *x = (*x) * (*x) * (*x;)	// alternate
    	// *x = pow(*x,3);		// alternate
    }
  54. void set_height(int feet = 6, int inches = 0);
  55. answer: d. The prototype in file2 is incorrect, but the compiler only "sees" one .cpp file at a time and therefore doesn't "know" about the error. When the linker runs (after the compiler component ends), it attempts to match the function call "bar(10)" with a function named bar that takes one integer argument (or an argument that the compiler can convert). The linker can't find a corresponding function and reports a LNK or loader error.
  56. answer: d. The prototype in file2 is incorrect, but the compiler only "sees" one .cpp file at a time and therefore doesn't "know" about the error. C++ is a case-sensitive language, so the linker can't match up the function call to "Bar" with the function definition bar, and the result is a LNK or loader error.
  57. answer: pass by reference and pass by pointer
  58. void func(int x, int y, int z); or void func(int, int, int);
  59. x and y are passed by value, and cannot be changed.
  60. Plese see yjr answer for sg05, question 21
    void swap(Foo& x, Foo& y)
    {
     	Foo temp = x;
    //Foo& temp = x; // doesn't work; why??
    x = y;
    y = temp; }

    What About Pass By Pointer?

    Questions 58 and 59 posed the same problem but used two of the three different argument passing techniques. Is it possible to solve that problem using the third pass-by-pointer technique? And the answer is yes, and the function is straightforward and similar to the pass-by-reference function. But the way that we must define the original objects and pass them is a bit more challenging:

    void swap(Foo* x, Foo* y)
    {
     	Foo temp = *x;
    *x = *y;
    *y = temp; } ----------------------------------------------------------------------- Foo f; Foo g; Foo* a = &f; Foo* b = &g; swap(a, b); // or Foo* a = new Foo; Foo* b = new Foo; swap(a, b);