Skip to content

return — give the result back

square(4) printed the result. But what if we want to use that result in another calculation? print writes to the screen and… it’s gone. return, on the other hand, gives the result back — so you can store it in a variable, add it up, or compare it.

Inputs (for input())

One value per line — read in order by input().

Output
 

The call add(3, 4) becomes the value 7 — as if 7 were written right there. That’s why add(10, 20) + add(1, 2) = 30 + 3 = 33 works. You couldn’t do this with a print-based function!

Remember: return ends the function immediately — any lines after it don’t run.

Now you know how len() works: it returns its result too. That’s why print(len(my_list)) nests neatly: len returns first, then print prints. You can already write functions like that yourself.

Mission

An add function

+15 XP

Write add(a, b) — it should return the sum. The checker will call the function itself to test it, so print won’t be enough — you really need return!

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Square 2.0

+15 XP

Rewrite square with return and print the sum of two squares: square(5) + square(3)34.

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Average grade

+20 XP

Write an average(numbers) function — it should return the average of the numbers. Test it with your own grades! (A new friend: sum() returns the total of a list.)

Inputs (for input())

One value per line — read in order by input().

Output
 

🏅 You finished this module! You now have all of Python’s core tools: variables, conditions, loops, collections, and functions. The final module is the proving ground: four real projects are waiting for you. On to the projects →