↩️ Return Values
A return value is the result that a function sends back to the place where it was called.
We use the return keyword to send a value back.
🔹 Basic Example
def add(a, b):
return a + b
result = add(10, 5)
print(result) #15🔹 return vs print()
print() displays a value, while return sends the value back.
def add(a, b):
return a + b
result = add(10, 5)
print(result) #15Because the function returns the value, we can store it, use it in another calculation, or pass it to another function.
def add(a, b):
return a + b
result = add(10, 5) * 2
print(result) #30🔹 Returning Multiple Values
A function can return multiple values.
def calculate(a, b):
return a + b, a - b
addition, subtraction = calculate(10, 5)
print(addition) #15
print(subtraction) #5Python returns them together as a tuple.
🔹 return Stops the Function
Once Python reaches return, the function immediately stops executing.
def check_age(age):
if age >= 18:
return "Adult"
return "Minor"print(check_age(20)) #Adult⭐ Remember
print() → Displays the result 🖥️
return → Sends the result back ↩️Use return when you want to use the result of a function somewhere else in your program.