Predict the Return Value Tracer
Run the Predict the Return Value Tracer MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim
A function hands a value back to whoever called it, and knowing that value in advance is what separates reading code from guessing at it. This MicroSim shows four short MicroPython functions with one call each, and asks you to type the return value before it reveals the answer. Two of the four use a default parameter value, and one returns two values at once as a tuple. By the end you should be able to run any of these calls in your head and demonstrate the right answer before the screen confirms it.
How to Use
- Pick one of the four functions from the Example dropdown.
- Read the highlighted call line and the argument values shown beside it.
- Type the value you think is returned, such as
Low,160, or64, 32. - Click Check Prediction. The Result panel reveals the real return value plus a short explanation of how the function got there.
- Click Next Example to move to the next function.
- Once you have tried two or more examples, a score line appears under the code. Click Reset to clear the score and start at example 1.
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Grade Level
9-12 (High School)
Duration
10-15 minutes
Prerequisites
- Know how to define a function with
defand call it by name. - Understand that indentation marks which lines belong to the function body.
- Be able to evaluate a comparison such as
level > 60as True or False. - Know that
//divides two integers and throws away the remainder.
Activities
- Exploration (5 min): Work through example 1. Trace the
if,elif, andelsebranches out loud, and say which one runs forlevel = 45and why the other two never execute. - Guided Practice (5 min): Compare examples 2 and 4, which call the same function. Predict both return values, then explain in one sentence what a default parameter value actually does.
- Assessment (5 min): Predict example 3 correctly on the first try, then
write down what
xandywould be if the call werescreen_center(240, 240).
Assessment
- The student predicts at least three of the four return values correctly.
- The student explains why
battery_status(45)returns"Low"and not"Full". - The student states what value
leveltakes whenset_brightness()is called with no argument. - The student explains that
return a, bhands back one tuple, which a caller can unpack into two variables.
References
- MicroPython Documentation - the official reference for the MicroPython language.
- Python tutorial: defining functions - covers parameters, default values, and return statements.
- Return statement (Wikipedia) - background on how functions hand values back to their callers.
- Tuple (Wikipedia) - the data structure behind returning two values at once.