Use `return` when a function has a result to give back. It ends the function call and makes the returned value available where the function was called. Unlike `print`, it does not display the value by itself. section 21.1 section 21.2
Return a length
```python def get_word_length(word1, word2): word = word1 + word2 return len(word) length = get_word_length("Rob", "Banks") print(length) ``` The function joins its two inputs, then returns the length of the joined word. The call is replaced by that number, so `length` can hold it. A statement placed after `return` in the function would never run. section 21.1 section 21.2
Keep these cases separate:
- A function that prints but has no explicit `return` still returns `None`. Printing that function call therefore displays `None` as well as anything printed inside the function. section 21.2
- To give back two results, return one tuple containing both, such as `return (add, sub)`. The caller can assign its elements to separate variables. section 21.2
- A function can even return another function. Return its name without parentheses to pass back the function object rather than call it. section 22.5
Listing 21.5. Function with and without a return
def say_name(kid): #1
print(kid) #2
def show_kid(kid): #3
return kid #4
say_name("Dora") #5
show_kid("Ellie") #6
print(say_name("Frank")) #7
print(show_kid("Gus")) #8
Compare a function that prints a name with one that returns it. Try predicting each call’s output before checking what happens when the calls are passed to `print`. section 21.2
A return value goes back to the calling statement; the function’s local variables do not go with it. Quick check: if you need to save a result for later, should the function print it or return it? section 21.2 section 22.1




