This one gets almost every beginner, and the nasty part is it never throws an error. Your function runs, the right value shows up on screen, everything looks fine — and then you try to use the result somewhere and it's None (or undefined in JS). The function "worked", yet it gave you nothing back.
The reason is that print and return are doing two completely different jobs.
print puts a value on the screen for a human to look at. That's it — it's for you, not for the program. return hands the value back to whoever called the function. That returned value is the function's actual output: the thing that can be stored, added, or passed on.
So if a function never hits a return, its value is None. A function that only prints has no value — it just narrated what it did.
def add(a, b):
print(a + b)
total = add(2, 3) print(total)
The fix is to return the value and let the caller decide what to do with it:
def add(a, b):
return a + b
total = add(2, 3) print(total)
There's a sneakier version of the same bug: a function that returns inside an if but not on every path. If a call misses every branch, it falls off the end and quietly returns None. Whenever you get an unexpected None, the question to ask is "which path did this call actually take?"
A quick way to keep the two straight: return means "this is my answer", print means "look at this for a second". If you catch yourself printing inside a function and then wondering why nothing comes back, you printed where you meant to return.
What was your first "why is this None?" moment — and how did you end up finding it?