return is not print
Mixing up return and print is the most common design bug in beginner functions, and the difference has teeth: a returned value can be stored, compared, tested, and fed into the next function, while a printed one is gone the moment it hits the screen. Getting return exactly right is what makes functions combine into bigger programs.
print shows a value to a human and gives nothing back. return hands a value to the calling code, silently. A function that computes should return, so callers can keep working with the result: store it, compare it, pass it onward.
Two more rules with teeth:
returnends the function instantly. Any code after the executed return never runs.- No return means
None. Fall off the end of a function and the caller receivesNone.
Rule 1 enables the tidy early return style, no else needed:
def classify(n): if n < 0: return "negative" if n == 0: return "zero" return "positive"
Reaching the last line already proves both ifs failed, the same top-to-bottom logic as your elif chains in lesson 4-1.
Code exercise · python
Run this. Each call takes a different exit from the function.
Quiz
What does this print? ```python def shout(word): print(word.upper()) result = shout("hi") print(result) ```
Code exercise · python
Your turn. Define `is_even(n)` that RETURNS True when n is divisible by 2, False otherwise. Then print `is_even(4)` and `is_even(7)`.
Problem
Trace by hand. What does this print? ```python def f(x): return x * 2 print(f(3) + f(4)) ```