greet(name) printed and that was it. What if the function should compute a value and hand it back to the caller — like a calculator function area(w, h)?
Can the function print the area? Then the caller sees it on screen.
Print and return are different things. print writes to the screen. return sends a value back to the code that called the function — so the caller can store it, pass it on, do anything with it.
def area(w, h):
return w * h
result = area(3, 4) # result = 12
print(result)So area(3, 4) is an expression that evaluates to 12?
Right. Once a function returns, its call expression becomes the returned value. You can use it anywhere a value goes — assign it, print it, pass it to another function:
print(area(3, 4)) # 12
total = area(3, 4) + area(5, 6) # 12 + 30 = 42What if I return halfway through? Does the rest of the function run?
No — return exits the function immediately. Anything below return is unreachable in that branch. Useful for early-exit patterns:
def abs_value(x):
if x < 0:
return -x
return xWhat if I forget the return?
The function returns None. No error — just an empty value. A common bug source: you call result = transform(x) and result is None because you forgot to write return before the value.
return — sending values outA function call is an expression. After the function runs, the expression evaluates to whatever the function returned:
def area(w, h):
return w * h
result = area(3, 4) # result is now 12A call to a value-returning function is just a value — use it however:
print(area(3, 4)) # pass it to print
x = area(3, 4) + area(5, 6) # use it in arithmetic
box = [area(2, 3), area(4, 5)] # put it in a listreturn exits immediatelyOnce return runs, the function ends. Code below isn't reached:
def classify(x):
if x < 0:
return "negative"
if x == 0:
return "zero"
return "positive"This pattern — a series of if + return lines — is cleaner than nested if/elif/else for many cases.
Return a tuple — a comma-separated list of values:
def minmax(numbers):
return min(numbers), max(numbers)
low, high = minmax([3, 1, 4, 1, 5]) # low = 1, high = 5The call returns a 2-tuple (1, 5); the assignment unpacks it into two names. Common pattern — Python doesn't restrict you to single returns.
returndef double(x):
x * 2 # missing `return`
result = double(5) # result is None — silent bugNo error. Just None. The function ran, computed x * 2, threw the result away, and ended. The caller gets None back. Always include return when the function is meant to produce a value.
print is not return| Statement | What it does |
|---|---|
print(x) | writes x to the console |
return x | sends x back to the caller |
A function that only prints can't be composed — its result vanishes to the screen. A function that returns can be chained, stored, tested. Default to return for anything you'll use later.
greet(name) printed and that was it. What if the function should compute a value and hand it back to the caller — like a calculator function area(w, h)?
Can the function print the area? Then the caller sees it on screen.
Print and return are different things. print writes to the screen. return sends a value back to the code that called the function — so the caller can store it, pass it on, do anything with it.
def area(w, h):
return w * h
result = area(3, 4) # result = 12
print(result)So area(3, 4) is an expression that evaluates to 12?
Right. Once a function returns, its call expression becomes the returned value. You can use it anywhere a value goes — assign it, print it, pass it to another function:
print(area(3, 4)) # 12
total = area(3, 4) + area(5, 6) # 12 + 30 = 42What if I return halfway through? Does the rest of the function run?
No — return exits the function immediately. Anything below return is unreachable in that branch. Useful for early-exit patterns:
def abs_value(x):
if x < 0:
return -x
return xWhat if I forget the return?
The function returns None. No error — just an empty value. A common bug source: you call result = transform(x) and result is None because you forgot to write return before the value.
return — sending values outA function call is an expression. After the function runs, the expression evaluates to whatever the function returned:
def area(w, h):
return w * h
result = area(3, 4) # result is now 12A call to a value-returning function is just a value — use it however:
print(area(3, 4)) # pass it to print
x = area(3, 4) + area(5, 6) # use it in arithmetic
box = [area(2, 3), area(4, 5)] # put it in a listreturn exits immediatelyOnce return runs, the function ends. Code below isn't reached:
def classify(x):
if x < 0:
return "negative"
if x == 0:
return "zero"
return "positive"This pattern — a series of if + return lines — is cleaner than nested if/elif/else for many cases.
Return a tuple — a comma-separated list of values:
def minmax(numbers):
return min(numbers), max(numbers)
low, high = minmax([3, 1, 4, 1, 5]) # low = 1, high = 5The call returns a 2-tuple (1, 5); the assignment unpacks it into two names. Common pattern — Python doesn't restrict you to single returns.
returndef double(x):
x * 2 # missing `return`
result = double(5) # result is None — silent bugNo error. Just None. The function ran, computed x * 2, threw the result away, and ended. The caller gets None back. Always include return when the function is meant to produce a value.
print is not return| Statement | What it does |
|---|---|
print(x) | writes x to the console |
return x | sends x back to the caller |
A function that only prints can't be composed — its result vanishes to the screen. A function that returns can be chained, stored, tested. Default to return for anything you'll use later.
Create a free account to get started. Paid plans unlock all tracks.