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




