Defining a Function with Parameters · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Name it once, call it anywhere

Computing · Programming · ages 15-16
Name ______________________   Date ____________
  1. For sayHello(person), what does the parameter let a caller do?

    • Supply a value for the body's named input
    • Choose a new function name whenever it is called
    • Skip the function body whenever it supplies a value
  2. What does defining a procedure give you?

    • A promise that every call runs faster
    • A guarantee that the program has no bugs
    • A reusable pack of steps under one name
  3. For the ordinary functions shown here, defining a function stores its body without running it. Calling the function runs that body.

    Circle one:   True   False

  4. reportGrade computes score as (numCorrect / 30) * 100. What score does a student earn with 18 correct answers?

    Answer: ______________

  5. offset(value) returns value + 4. What value does offset(8) return?

    Answer: ______________

  6. Using positional arguments, which call supplies text then a number to reportGrade(studentName, numCorrect)?

    • reportGrade("Lena")
    • reportGrade("Lena", 21)
    • reportGrade(21, "Lena")
  7. Six greeting blocks use the same steps but greet different people. All six greetings must remain. Which change removes copied steps and keeps the different names?

    • Use one fixed-name greeting function at all six locations
    • Use one function with a name parameter and six calls
    • Keep only one greeting block and remove the other five uses
  8. The complete Python header is def move(x, y):. These are its only parameters, with no defaults. A call supplies three positional arguments. What happens?

    • Python rejects the call before running its body
    • Python stores the third argument for the next call
    • Python repeats the body to use the third argument
LightMySky · lightmysky.comW1-mt_pyo7yBg4H3-s1

Answer key

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

Name it once, call it anywhere W1-mt_pyo7yBg4H3-s1

  1. Supply a value for the body's named input · person names an input. Each call supplies the value that the greeting body uses.
  2. A reusable pack of steps under one name · A procedure groups reusable statements under a name. It does not guarantee speed or freedom from bugs.
  3. True · A definition supplies reusable code. A later call executes the body.
  4. 60 · 18 / 30 = 0.6. Multiplying 0.6 by 100 gives a score of 60.
  5. 12 · The argument 8 fills value. The return expression computes 8 + 4 = 12.
  6. reportGrade("Lena", 21) · The call supplies "Lena" to studentName and 21 to numCorrect, in the required positional order.
  7. Use one function with a name parameter and six calls · One parameterized function shares the steps. Six calls supply the six required names without removing any greeting.
  8. Python rejects the call before running its body · The complete definition accepts exactly two positional arguments. There is no optional third parameter or extra-argument parameter.
Worksheet · LightMySky