Chapter 6

Contents: True/False | Multiple Choice | Discussion | Programs

True/False

  1. False
  2. False
  3. True
  4. True (None is returned when no explicit value is provided)
  5. False (Technically, all are passed by value, but those value may be mutable)
  6. False
  7. False (Mutable parameters can be changed)
  8. True
  9. True
  10. False

Multiple Choice

  1. b
  2. a
  3. a
  4. b
  5. d
  6. a
  7. d
  8. a
  9. d
  10. a

Discussion

  1. The two main motivations are reducing duplication and making the code ore modular. These can be described in a variety of ways.
  2. Both the calling code and the function definition itself can be thought of as sequential. Although the program now seems to "jump around" to execute various function definitions, each part can still be considered as sequential. This is a good springboard for discussing levels of abstraction.
  3.  
    1. The main purpose is to provide information from the caller to the function. Parameters provide the changeable context of a function call.
    2. Formal parameters are variables in the function definition. Actual parameters are the values given to those variables when control transfers to the function.
    3. Parameters are like ordinary function variables in that they are local to the function and serve as "name values." They are different because they are automatically assigned an initial value when the function is called. Normal local variables have to be assigned a value within the function definition.
  4.  
    1. Parameters can be used as input to the function.
    2. Usually through a return statement. Functions may also change the values of mutable parameters.
  5.  
    1. Returns the cube of its argument.
    2. cube(y)
    3. There are two different variables named answer. The variable inside of cube is changed, but it does not affect the value of the other variable.

Programming Exercises

Code for Chapter 6 Exercises