liveBook · Manning

Return statement

As explained in Learn AI-Assisted Python Programming, Second Edition

A return statement ends a function execution and optionally provides a value as the function's output.

A return statement sends a function’s answer back to the line that called it. It lets a function turn its inputs into a useful output. section 3.1.1 section 3.8

Use return inside a small, well-defined function. In a larger task, separate subtasks into functions so each result can be tested and reused. section 3.8

Returning the larger number

The larger function compares two parameters. It returns num1 when num1 is greater; otherwise, it returns num2. Calling larger(2, 5) produces 5, which another line can store or use. section 3.1.1

# write a function that returns the larger of two numbers
# input is two numbers
# output is the larger of the two numbers
def larger(num1, num2):          #1
    if num1 > num2:        #2
        return num1        #2
    else:                  #2
        return num2        #2

Compare the two branches and see how each return statement supplies the function’s output. section 3.1.1

When working with returned values:

  • Pass arguments that match the function’s parameters. section 4.3.1
  • Store the returned value in a variable when you need it later. section 4.3.1
  • Test the function with different types of inputs. section 3.8

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains return through function outputs, conditional examples, and calls, but does not detail execution internals.
Examples
Uses a larger-number function, Python’s max function, and input returning a user’s typed name.
What it assumes
Assumes basic function calls and values while teaching functions, parameters, outputs, and testing through practical examples.
What you can do after
Apply it via Python functions to send computed answers back, store results, and test behavior across inputs.
How it connects
Connects return with functions, parameters, arguments, function signatures, problem decomposition, and unit testing.

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 Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.