Return Values and Variable Scope · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.
به انگلیسی

Return values and local names

Computing · Programming · ages 15-16
Name ______________________   Date ____________
  1. In the lesson's ordinary Python functions, what happens when return executes?

    • The current call restarts with its arguments
    • The caller stops without using the returned result
    • The call ends and its caller continues
  2. Why would a function return a computed result?

    • To display the result on screen
    • To pass the result back for later use
    • To speed up every statement in its body
  3. A Python function can print a message and also return a value to its caller.

    Circle one:   True   False

  4. A Python function's body has these statements in order: print("Open"); return 7; print("Close"). The caller calls it without printing its result. What appears?

    • Open only
    • Open and Close
    • 7 only
  5. ticket_total(count) returns count * 3. What value does ticket_total(7) return?

    Answer: ______________

  6. A Python function assigns its local total = 17. There is no total outside that function. After the call, outside code runs print(total). What happens?

    • It displays 17 from the function's local total
    • An error: total is not defined outside
    • It creates an outside total holding zero
  7. The caller needs one computed score in three later calculations, without copying screen output or using a global variable. Which design fits?

    • Return the value and store it once
    • Print the value and retype it once
    • Make the value global and reuse it
  8. A Python function has these body statements in order: return points; points = 18. There is no earlier local assignment. It should return 18. Which repair fits?

    • print(points); points = 18
    • return points; points = 18
    • points = 18; return points
LightMySky · lightmysky.comW1-mt_jivtTHpZc7-s1

Answer key

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

Return values and local names W1-mt_jivtTHpZc7-s1

  1. The call ends and its caller continues · Return ends the current function call and passes control back to its caller.
  2. To pass the result back for later use · Returning passes a result to the caller, which can store it or use it in another expression.
  3. True · Printing and returning have different jobs. One function can do both.
  4. Open only · The function displays Open, then returns 7. The later print is skipped, and the caller does not print the returned number.
  5. 21 · The argument makes count equal to 7. The return expression computes 7 * 3 = 21.
  6. An error: total is not defined outside · The local assignment does not create an outside binding named total. The outside print cannot find that name.
  7. Return the value and store it once · Storing the returned value gives the caller the computed value for all three calculations and meets both restrictions.
  8. points = 18; return points · The assignment makes points local. It must run before return reads that local name. The repaired function returns 18.
Worksheet · LightMySky