Return Values
Prerequisites: Functions
return sends a value back out of a function to whatever called it — a
function without a return sends back None.
def add(a, b):
return a + b
result = add(2, 3)
print(result)
Expected output
5
Common mistakes
- Calling
print()inside a function instead ofreturning the value — that only displays the result, it doesn't hand it back for the caller to store or use further.
Practice
Write a function square(n) that returns n * n, then print
square(6).
Show solution
def square(n):
return n * n
print(square(6))