Check your understanding of the previous reading: the substitution model, parameter passing, call frames, and how data flows between functions.
True or False: You can use the return statement to return a variable from a function.
Select one answer, then check.
return statement substitutes the function call with a value, never with a variable name from inside the function.Re-read "Scope and Values": functions consume and return values, not variables. Ask what actually gets substituted back into the caller when a function returns.
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.
Look at the "Substitution" section: "When you call a function, each parameter is assigned the value of a relevant argument."
True or False: The print function is necessary to call a function.
Select one answer, then check.
print is just another, separate function you can call on the result if you want to see it in the console.calculate_grade(raw, weight) runs and returns a value whether or not it is ever passed to print.Look at "Data between Functions": calculate_grade(raw, weight) is called and its result stored in grade, with no print in sight.
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.
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.Re-read "Functions Calling Functions": every call gets its own frame, even calls to the same function or with identically-named parameters.
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.
fix_capitalization(name) is substituted directly as the argument to make_message, so the fixed-capitalization value is what flows in.fix_capitalization(name) is thrown away here, nothing captures it. make_message(name) still receives the original, unfixed name.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.fix_capitalization(name) shows the fixed text, but it is never fed into make_message; the second call still uses the original, unfixed name.For each option, trace whether the fixed-capitalization value actually reaches make_message, either by nesting the call directly or by reassigning name first.
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.
| Expression | Value that fills the blank |
|---|---|
| print(x(y(z(___)))) | |
| print(x(y(___))) | |
| print(x(___)) | |
| print(___) | |
| ___ |
Work from the inside out, one function call at a time.
z(3) runs first: 3-1.y(...) runs on that result: ...*2.x(...) runs on that result: 6+....x(...) returns is what print actually prints.print around its blank. Think about what the print call itself is replaced by, once it has run.Substitution, one step at a time:
| Expression | Value |
|---|---|
print(x(y(z(3)))) | 3 (the original argument to z) |
print(x(y(2))) | 2, since z(3) is 3-1 |
print(x(4)) | 4, since y(2) is 2*2 |
print(10) | 10, since x(4) is 6+4 |
None | None, since print writes 10 to the console and then returns None |
The last row has no print(...) wrapped around its blank, because by that step the print call itself is being substituted. A call is replaced by what it returns, and print returns None.