Return Values
Concepts covered: Functions
print_fare from the first section shows a number on the screen and that is the end of it: the program cannot add that fare to another, compare it with a limit or store it for a receipt. The return statement hands a value back to the line that made the call, where it can be used like any other value. The 2 functions below compute the same number and differ only in what they do with it:
def show_fare(miles):
print(250 + miles * 175)
def fare(miles):
return 250 + miles * 175
a = show_fare(2)
b = fare(2)
print("a is", a)
print("b is", b)Output:
600 a is None b is 600
The call show_fare(2) printed 600 as a side effect but handed nothing back, so a holds None, the value Python uses for the absence of a value. Any function that reaches the end of its body without meeting a return hands back None. The call fare(2) printed nothing and handed back 600, which the assignment stored in b.
- print(value)
- Sends text to the screen for a person to read
- The calling line receives None
- Belongs where a person needs to see the value: a receipt line, a progress report, a final answer
- return value
- Sends the value to the calling line for the program to use
- The calling line receives the value
- Belongs in every function whose result feeds another step
Using the returned value
A returned value can be stored in a variable, added to another, or multiplied right there in the calling expression. Two trips are priced, summed and surged in the run below:
def fare_cents(miles, minutes):
return 250 + miles * 175 + minutes * 35
short_trip = fare_cents(1, 6)
airport_run = fare_cents(8, 25)
print("Short trip:", short_trip)
print("Airport run:", airport_run)
both = short_trip + airport_run
print("Both trips:", both)
surge_total = fare_cents(3, 12) * 2
print("Surge trip:", surge_total)Short trip: 635 Airport run: 2525 Both trips: 3160 Surge trip: 2390
fare_cents(3, 12) evaluates to 1195 inside the last expression, and the multiplication by 2 happens on that number before surge_total is assigned. A call that returns a value is an expression, so it fits anywhere a number fits.
return ends the function at once
When Python reaches a return, the function stops immediately, even if more lines follow. A function with several return statements inside if blocks therefore hands back the value of the first condition that holds. Step through the fare bands and count how many conditions each call tests:
def fare_band(cents):
if cents < 0 or cents > 50000:
return "Invalid fare"
if cents >= 4000:
return "Premium"
if cents >= 2000:
return "Long"
if cents >= 1000:
return "Standard"
if cents >= 500:
return "Short"
return "Minimum"
print(fare_band(4200))
print(fare_band(2525))
print(fare_band(1195))
print(fare_band(300))
print(fare_band(-50))
print(fare_band(60000))Premium Long Standard Minimum Invalid fare Invalid fare
The fare 4200 passes the range check, meets cents >= 4000 and leaves with Premium without touching the rest; 300 fails every band check and falls through to the final return. The order of the checks carries the logic: because 4200 is also at least 2000, swapping the Premium and Long checks would label it Long. The range check sits first for the same reason, so that 60000 is rejected before cents >= 4000 can claim it.
def tip_cents(fare):
return fare * 15 // 100
print("this line never runs")
print(tip_cents(2000))Output:
300
The print after the return is unreachable: no call can ever execute it, and leaving it there misleads the next reader into expecting output that never comes.
When None reaches arithmetic
The mistake that follows from confusing the 2 statements is using the result of a printing function in a calculation. The program below prints the total, then tries to add 100 to what the call handed back:
def show_total(cents):
print("Total:", cents)
result = show_total(1195)
print(result + 100)The program prints Total: 1195, then stops with TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. result holds None, and + has no meaning between NoneType and int. The fix is one line: replace the print with return cents, and let the caller print when it wants to.
What does this program print?
def grade(points):
if points >= 50:
return "pass"
return "fail"
print("done")
print(grade(80))
print(grade(20))- pass, then done, then fail
- pass, then fail, then done
- pass, then None
- pass, then fail
The value after return can be any expression, from a single variable to a whole calculation. Python computes the expression first, using the parameter values from this call, and then hands back the result: return miles * 175 with miles holding 4 hands back 700. The caller never sees the expression, only the number it produced.
About this lesson
This section is part of the Functions: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.