The Return Path
Once Hoppy reaches the craft bench, the first problem is not a bigger machine. It is a half-lit path. Each test run can calculate the light value inside a function, but the number only flashes there for a moment. The code outside the function never gets to keep it.
That is the tiny job of return: a function does not only show a result. It can hand that result back to the caller, so the outside code can keep using it.
print() shows something, but return hands it back
If you use print() inside a function, you only let a person see the value. Once the function ends, that value is not automatically available for the next line outside. return is different: it actually sends the result back to the place that called the function.
glow_dust = 2
moss_drops = 4
bonus_light = 1
def brew_path_light(glow_dust, moss_drops):
total_light = glow_dust + moss_drops
return total_light
path_light = brew_path_light(glow_dust, moss_drops)
brighter_path_light = path_light + bonus_light
print("Path light:", path_light)
print("Brighter path light:", brighter_path_light)
In this code, brew_path_light() calculates one light value, then uses return to send it back out. That lets path_light store the result, and then brighter_path_light can build on it with one more step.
Store the returned value, then the next step can begin
The key idea to keep from this lesson is simple: print() is more like “show it to me,” while return is more like “hand it back to the code outside.” So today you will not only write return. You will also store the returned value in a variable and use it again.
The starter already calculates total_light inside brew_path_light(). Your job is to stop only printing it and instead use return total_light.
Use path_light = brew_path_light(glow_dust, moss_drops) so the outside code keeps the result after the call finishes.
Build brighter_path_light from path_light and bonus_light. This is the main proof for today: a returned value can keep doing work after the function call ends.
print() means “let me see it.” return means “hand the result back.” If you want code outside the function to keep using that result, think of return first.
Suggested SolutionExpandCollapse
glow_dust = 7
moss_drops = 5
bonus_light = 3
def brew_path_light(glow_dust, moss_drops):
total_light = glow_dust + moss_drops
return total_light
path_light = brew_path_light(glow_dust, moss_drops)
brighter_path_light = path_light + bonus_light
print("Path light:", path_light)
print("Brighter path light:", brighter_path_light)Advanced TipsWant more? Click to expandClick to collapse
Today you are strengthening one basic but very important reflex: a function can hand one result back to its caller. Once you store that result in a variable, it stops being a value that only flashes on screen and becomes something the next lines can really use.
In the next lesson, you will see that sometimes one small job naturally hands back two related results together. For now, this one return path is enough.