functions and the missing return
Functions — the unit AI organizes everything around
Once AI moves past three or four lines of code, it stops writing scripts and starts writing functions. Validate the email. Compute the score. Format the response. Each of those becomes a named bundle of logic that takes inputs, does work, and hands back an answer.
This is the single biggest shift between "I can read variable assignments" and "I can read AI's actual code." Almost every Python file Cursor writes is a stack of functions calling other functions. If you can read one, you can read the file.
The four moving parts
Look at the editor on the right and find each piece:
def double(n):
return n * 2
print(double(7))
def double(n):— the contract. You're declaring a function nameddoublethat accepts one input, which it will calln. The colon opens the body.return n * 2— the deliverable. When the function is called, it computesn * 2and hands that value back to whoever called it.double(7)— the call. You're saying run that function, and setnto7for this run. Python evaluates the body, hitsreturn, and replaces the wholedouble(7)expression with the returned value.print(...)— wraps the call so you can see what came back.
Read it again, slower. The function definition is a recipe. The call is cooking with that recipe one specific time. They are not the same moment in the program's life.
Why this is the move AI reaches for constantly
Functions are how AI deduplicates. The instant the same three-line
pattern needs to happen twice — even slightly differently — Cursor will
extract it into a function. That's why almost every AI-generated file
opens with a stack of def blocks before any "real" code runs.
Two reading habits to build now:
- At the
defline, read the parameters. They tell you what the function needs. If you seedef fetch(url, headers, retries):, you know the call site has to supply three things. - At the
returnline, read the type. A function ending inreturn resultwhereresultis a list returns a list. That answers "what do I do with the value the function gave me?"
Where AI specifically gets functions wrong
The most common AI bug in this chapter is forgetting return. Cursor
will write a function that prints the answer and assumes it's done —
but the caller gets back None, not the value. The next chapter
focuses on this exact trap, because you will see it in real code this
week.
Run the editor. double(7) returns 14, and the print shows it.