Python Basics for Vibe Coders, Part 7: Functions, and Why print Is Not return
Python functions: def, parameters and defaults, print vs return, why no return means None, and how to read and test AI code one function at a time.
Open code an AI wrote for you and you'll see several blocks that start with def. Those are functions. Once you can read functions, even long code breaks into chunks you can follow: "this part calculates tax, that part saves the result." This is Part 7 of the 10-part series "Python Basics for Vibe Coders," and it covers def, parameters and default values, and the difference between print and return that trips up so many beginners. The examples are written for Python 3.15, and I ran them in Python 3.14.2 to check the results.
Key Takeaways
- A function is a named block of code that takes input and gives back a result. You create one with
def name(parameters):.returnhands a value back to the code that called the function. They are not the same.- A function without
returngives backNone.- A parameter with a default value, like
rate=0.1, can be left out when you call the function.
A function is a vending machine
Put in money, press a button (input), and a drink comes out (result). You can use it without knowing what happens inside. Functions work the same way: if you know the name, the input, and the result, you can use one.
People also compare functions to blenders, but that analogy has a gap. A blender pours its result right in front of you. A function's return only hands the value back to the caller; nothing shows up on screen. To see it, you have to print it separately.
Defining and calling a function
def add_tax(price, rate=0.1):
return round(price * (1 + rate))
print(add_tax(1000))
print(add_tax(1000, 0.05))
def say_hi(name):
print(f"Hi, {name}!")
result = say_hi("Sam")
print(result)
1100
1050
Hi, Sam!
None
Here's how to read it.
def add_tax(price, rate=0.1):means "create a function namedadd_taxthat takespriceandrate." The names inside the parentheses are called parameters.rate=0.1is a default value. If you leave outrate, as inadd_tax(1000), 0.1 is used.return round(...)rounds the result and gives it back. Note that Python'sroundrounds an exact .5 to the nearest even number (round(2.5)is 2). Here, 1000 × 1.1 rounds to 1100, soprint(add_tax(1000))prints 1100.say_hionly prints a greeting and gives nothing back. That's whyresultisNone.
Common trap: does printing a value mean you returned it?
When you run say_hi("Sam"), "Hi, Sam!" appears on screen, so it feels like you got that text back. But print result and you get None. What showed up on screen and what the function gave back are different things.
This difference causes real problems when you fix AI code. Say the AI wrote a function that only prints an average, and other code tries to use that average to assign a grade (the conditionals from Part 3). Now you're comparing None to a number, and you get an error. The fix follows two rules:
- Values that other calculations need should be given back with
return. - Print to the screen at the end, from the code that calls the function.
How to read functions in AI code
- Read the name. Many names start with a verb, like
load_data,calculate_total, orsave_result. Guess what the function does from the name alone. - Check the input. Look at the parameters in the parentheses and whether any have default values.
- Find the
return. See what it gives back. If there's noreturn, it gives backNone. - Test it on its own. Pull out one function and call it, like
print(add_tax(1000)). You can check whether that function works without running the whole long program.
Asking the AI function by function also gives cleaner results: "Write an average(nums) function that calculates the average. Give the value back with return, and do the printing outside the function."
Check yourself
- If you call a function that only
prints and has noreturn, as inresult = say_hi("Sam"), what isresult? →None. It's easy to mistake the text on screen for the returned value. - If you call
def add_tax(price, rate=0.1)asadd_tax(1000), what israte? → 0.1. Parameters with default values can be left out.
Try it yourself
Write an average(nums) function that takes a list of numbers and returns the average. If average([80, 90, 100]) gives 90.0, you've got it. If you get stuck, use sum (the total) and len (the count), and make sure you used return, not print.
def average(nums):
return sum(nums) / len(nums)
print(average([80, 90, 100]))
90.0
Previous and next
- Previous: Part 6: Dictionaries, Looking Up Values by Key, and KeyError
- Next: Part 8: Reading Errors covers how to read the error messages you get when a function fails, and how to pass errors to the AI the right way.
The full series list is at the end of Part 10.