liveBook · Manning

Return statement

As explained in Get Programming: Learn to code with Python

A statement that terminates a function's execution and optionally supplies a value to its caller.

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

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains early termination, implicit `None`, tuple results, and returning function objects without detailing interpreter internals.
Examples
Contrasts printing and returning names, returns a joined string’s length, and unpacks addition and subtraction results.
What it assumes
Introduces Python function definitions and parameters before using returns; later function-object examples assume those basics.
What you can do after
Build it from scratch: write functions whose results can be assigned, printed, or unpacked by a caller.
How it connects
Connects function calls and scope with printing, tuples, and nested functions.

Also explained in

Compare how 5 books explain return statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.