functions and the missing return
return and print are not the same — and AI confuses them constantly
This is the highest-frequency bug in any AI-generated function under twenty lines. Two things that look similar to a non-coder do completely different jobs, and Cursor mixes them up the moment you ask for "a function that gives me back the result."
You will hit this bug. Probably this week. Read this until the distinction is automatic.
What each one actually does
print(x)— writesxto the screen, full stop. It's a side-effect. The function keeps running. Onceprintis done, the string is just gone — nothing in the program "has" it.return x— handsxback to whoever called the function. The function stops at that point. The value flows back into the call site as the result offunction_name(...).
The two often produce the same visible output in a quick demo,
which is exactly why the bug hides. A function that prints "HELLO"
on the screen and a function that returns "HELLO" look identical
when you call them at the top level. They are not.
The worked example
The editor on the right has both versions side by side:
def shout_returns(word):
return word.upper()
def shout_prints(word):
print(word.upper())
a = shout_returns("hello")
b = shout_prints("hello")
Trace what happens:
shout_returns("hello")runs"hello".upper()→"HELLO", thenreturnhands that string back.ais now"HELLO".shout_prints("hello")runs"hello".upper()→"HELLO", thenprintwrites it to the screen. The function ends without areturn, so it implicitly hands backNone.bis nowNone. The screen showsHELLO, butbitself isNone.
When you then print("b is:", b), you see b is: None. The shout
function "worked" — it shouted — but the value is unrecoverable.
Why this is the bug AI ships most often
When you ask Cursor for a function that capitalizes the user's name
or a function that doubles the score, it has a coin flip in front of
it: return or print. If the prompt sounds like "display the result"
or "show me the answer", AI tilts toward print — and now you have a
function that you can't compose with anything else. You can't store its
output, can't pass it to another function, can't write it to a file.
The information is visible but not captured.
The fix is almost always: change the print to a return. If you
also need to see it on screen, the caller prints what the function
returned.
result = shout_returns("hello")
print(result)
Two lines, but the result is now a real value you can use again.
How to catch this when reading AI code
Skim the body of every function for two things:
- Is there a
return? If not, the function returnsNone. - Does the
returncome at the end of the path you care about? Areturninside anifand not in theelsemeans one branch gives you a value and the other gives youNone.
Run the editor and watch b is: None print out. That's the bug
captured in nine lines.