5.13.  Review Questions

[ fromfile: functions-questions.xml id: functions-questions ]

  1. What is the difference between a function declaration and a function definition?

      A declaration is for describing how a function can be called. A definition includes the actual code that is to be executed.

  2. Why are default argument specifiers in the declaration but not the definition?

      Because it describes how the function can be called (interface), not how the function is implemented.

  3. Explain why is it an error to have two functions in the same scope with the same signature but with different return types.

  4. For overloading arithmetic symbols (+ - * /) on Fraction objects, which is preferred, member functions or nonmember global operators? Explain your answer.

      Nonmember global opreators are preferred because then the first or second operand can be converted just as easily to a Fraction. As a member function, only the right hand side is convertible, which means the operator won't work in a symmetric way.

  5. For overloading left-modifying operators such as = and +=, which is preferred, member function operators or (nonmember) global operators?

      Because you can and should return *this, which is not a reference to a temporary, a member function operator is the most appropriate. This permits chaining.

  6. Explain the difference between pass-by-value and pass-by-reference. Why would you use one instead of the other?

     

    A value parameter is a local variable in a function that holds a copy of a particular argument object that was passed to the function. Changes to that variable due to the action of the function have no effect on the argument object.

    A reference parameter is an alias for a particular argument object that was passed to the function. No copy is made of the argument object. The alias is local to the function and, if it is not declared const, can permit the action of the function to change the argument object.

  7. Explain the difference between preprocessor macros and inline functions.