Functions at Depth: Default and Keyword Arguments · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

One function, many ways to call it

Computing · Programming · ages 16-17
Name ______________________   Date ____________
  1. What does greeting(count=2, name="Ash", msg="Hiya") print?

    • Hiya Ash, twice
    • Ash Hiya, twice
    • Hiya Ash, once
  2. In def greeting(msg, name="Friend", count=1), which call uses the default for name?

    • greeting("Hi", "Ash", 3)
    • greeting("Hi")
    • greeting("Hi", "Ash")
  3. In greeting("Welcome", count=1, name="Anita"), the value "Welcome" is a positional argument.

    Circle one:   True   False

  4. With def greeting(msg, name="Friend", count=1), what does greeting(count=0, msg="Hello") print?

    • nothing
    • Hello Friend
    • an error message
  5. Which of these calls produces an error?

    • greeting("Morning", "Morgan", count=3)
    • greeting("Cheers", "Colleagues", 10)
    • greeting(count=1, "Hi", "Bea")
  6. A function appends each caller to a default list log. The first call adds "Ann" and the second adds "Ben". What does log hold after the second call?

    • ["Ben"]
    • ["Ann", "Ben"]
    • []
  7. Which call follows the recommended spacing around the equals sign?

    • greet(name ="Ash")
    • greet( name="Ash" )
    • greet(name="Ash")
  8. Mia writes def add_item(item, box=[]) that appends item to box and returns it. She calls add_item("pen") and then add_item("cup"). What is wrong?

    • the second call returns ["pen", "cup"]
    • the second call returns ["cup"]
    • both calls fail with an error
LightMySky · lightmysky.comW1-mt_SR8RhRmYhz-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

One function, many ways to call it W1-mt_SR8RhRmYhz-s1

  1. Hiya Ash, twice · Names decide where each value goes, so msg is "Hiya" and the count of 2 prints it twice.
  2. greeting("Hi") · Only the second call skips name, so "Friend" stands in for it.
  3. True · It names no parameter, so it matches msg by place.
  4. nothing · The count says print zero times, so the function runs but outputs nothing.
  5. greeting(count=1, "Hi", "Bea") · Positional values must come first, and here two of them follow a keyword one.
  6. ["Ann", "Ben"] · The default list is shared, so "Ann" from the first call is still inside.
  7. greet(name="Ash") · Style is no spaces around the equals sign for keyword arguments and defaults.
  8. the second call returns ["pen", "cup"] · The same default list serves both calls, so the second result carries the first call's item.
Worksheet · LightMySky