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?"
The hallucinated return
Here is the bug this lesson exists for. AI writes functions constantly,
and the silently missing return is one of its best-documented failure
modes. The function reads like it hands the answer back — the work
happens, the variable gets computed, sometimes a print even shows the
value on screen — but the line that actually delivers the value to the
caller was never written. The return you'd swear is there isn't. The
caller gets None, and your code fails three steps later with an error
pointing at the wrong place.
The rest of this lesson drills spotting that missing return on sight, because you will see it in real code this week.
Run the editor. double(7) returns 14, and the print shows it.