Beginner · 12 min
Returning values
Return a function result and use it in another calculation.
Give a result back
return sends a value back to the caller. Unlike print(), it does not display the value. Store or print the returned value where you call the function.
def add_fee(amount):
return amount + 2
total = add_fee(10)
print(total)Output
12
Use more than one parameter
Separate parameters and arguments with commas. return ends the current function call. Here, two inputs produce a value that can be reused.
def cost(price, quantity):
return price * quantity
subtotal = cost(3, 4)
print(subtotal + 2)Output
14
Returning values
- Track the returned value back to the caller.
- Distinguish returning a value from displaying it.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Return a total
Complete cost(price, quantity) so it returns the product. Keep the supplied call, store its result in total, and display it.
Return price * quantity from the function.