Check your understanding of the previous reading: the substitution model, parameter passing, call frames, and how data flows between functions.

Question 1

True or False: You can use the return statement to return a variable from a function.

Select one answer, then check.

  • Correct. Functions do not consume or return variables; they consume and return values. A return statement substitutes the function call with a value, never with a variable name from inside the function.
  • Not quite. A local variable only exists inside the function's own frame; once the function returns, that frame is gone. What comes back to the caller is the value the variable held, not the variable itself.

Question 2

True or False: Values enter functions as arguments when the function is called, and then the arguments' values are assigned to parameters when the function's body begins executing.

Select one answer, then check.

  • Correct. This is exactly the substitution rule from the reading: when you call a function, each parameter is assigned the value of the matching argument before the function body runs.
  • Not quite. This is precisely how parameter passing works: the argument's value flows in and is assigned to the parameter name before the body starts executing.

Question 3

True or False: The print function is necessary to call a function.

Select one answer, then check.

  • Correct. Calling a function only requires writing its name followed by parentheses with the right arguments. print is just another, separate function you can call on the result if you want to see it in the console.
  • Not quite. Calling a function and displaying its result are separate steps. calculate_grade(raw, weight) runs and returns a value whether or not it is ever passed to print.

Question 4

True or False: the two functions below share a variable named score.

def increase(score: int) -> int:
    return score+1
def decrease(score: int) -> int:
    return score-1

Select one answer, then check.

  • Correct. Each function call gets its own frame. increase's score parameter and decrease's score parameter live in separate frames that just happen to use the same name; changing one never affects the other.
  • Not quite. "Each function call's frame is separate from all other frames, even if they come from the same function or have variables with the same name." A shared name is not a shared variable.

Question 5

Given the program below,

name = input("What is your name?")
def fix_capitalization(name: str) -> str:
    return name.title().strip()
def make_message(name: str):
    return "Hello" + name + "how are you?"

which of the following will have the data flow correctly through both functions in order to produce the message with the fixed capitalization? Select every one that works.

Select every option that correctly chains the two functions, then check.

  • Correct. The result of fix_capitalization(name) is substituted directly as the argument to make_message, so the fixed-capitalization value is what flows in.
  • Not quite. The return value of fix_capitalization(name) is thrown away here, nothing captures it. make_message(name) still receives the original, unfixed name.
  • Correct. Reassigning name to fix_capitalization(name)'s return value updates it to the fixed-capitalization text, so the following make_message(name) call receives the fixed value.
  • Not quite. Both functions require an argument. Calling them with no arguments at all raises an error, it does not move any data.
  • Not quite. Printing fix_capitalization(name) shows the fixed text, but it is never fed into make_message; the second call still uses the original, unfixed name.
  • Not quite. Sharing a parameter name is not the same as sharing a value. Without using return values and arguments, data cannot move between functions.

Question 6

Given the program below:

def x(a: int) -> int:
    return 6+a
def y(a: int) -> int:
    return a*2
def z(a: int) -> int:
    return a-1
print(x(y(z(3))))

Match each blank to the value that will be substituted there as the last line is executed. The same value can be the answer for more than one row.

ExpressionValue that fills the blank
print(x(y(z(___))))
print(x(y(___)))
print(x(___))
print(___)
___