DeferredReturn
A return
statement in a function does not return immediately, but it waits until the function finishes executing.
A return statement in the middle of a function doesn't return immediately
A return statement immediately returns from the function
CorrectionHere is what's right.
A return
statement returns immediately, for example:
def tick(value: int):
if value <= 0:
return
value -= 1
if value == 0:
ring_bell()
In the above code, if value <= 0
, then the return
statement executes and returns from the function immediately, i.e., lines after the return statement will not be executed.
Note for advanced students: The only situation where return
does not return immediately is if the return
statement is located inside a try
block with a corresponding finally
block. In that case, the finally
block will execute before returning.
ValueHow can you build on this misconception?
This misconception can provide a good opportunity to discuss statement sequences as a key aspect of imperative programming.
The misconception can also be an opportunity to point out the fact that return
statements located inside a try
block with a corresponding finally
block indeed do not return immediately. However, a full-fledged discussion of the details of exception handling may be too advanced at the time students hold this misconception.