This is a great question and one of the most important concepts to master in programming!
Think of the difference like this: print() is for humans, but return is for the program.
1. print() is a display
When you use print(), you are just showing a result on the screen so that you (the human) can see it. The computer "forgets" that value immediately after it's shown. You cannot use a printed value for any further calculations.
2. return is a hand-off
When a function uses return, it sends a piece of data back to the part of the program that called it. The program "remembers" this value. You can store it in a variable, add it to another number, or pass it into a different function.
Comparison Example:
Using print() (The value is "trapped" on the screen):
def add_and_print(a, b):
print(a + b)
# This shows 10 on the screen
add_and_print(5, 5)
# This will FAIL because the function didn't "give back" a number
# result = add_and_print(5, 5) + 20 <-- Error!
Using return (The value is passed back to the program):
def add_and_return(a, b):
return a + b
# Nothing shows on the screen yet...
result = add_and_return(5, 5)
# ...but now we can use that data later!
final_score = result + 20
print(final_score) # Output: 30
Key Differences at a Glance:
| Feature | print() |
return |
|---|---|---|
| Purpose | To show information to the user. | To provide data to the rest of the code. |
| Visibility | You see it in the terminal/console. | It is invisible unless you specifically print the result. |
| Reusability | Cannot be stored or used in math. | Can be stored in variables and used later. |
| Function Flow | A function can print many times. | return ends the function immediately. |
Labby's Tip: In professional coding, most functions use return because we usually want to do something with the data, not just look at it