What is the main purpose of the return statement in a Python function?
return
Consider this function: def add_numbers(x, y): result = x + y return result How would you store the value returned by calling add_numbers(5, 3) into a variable named total?
def add_numbers(x, y):
result = x + y
return result
add_numbers(5, 3)
total
print(add_numbers(5, 3))
result = add_numbers(5, 3)
total = add_numbers(5, 3)
add_numbers(5, 3) -> total
What happens if a function doesn't have an explicit return statement?
None
What will be printed by the following code? def greet(name): message = "Hello, " + name # No return statement here result = greet("Alice") print(result)
def greet(name):
message = "Hello, " + name
# No return statement here
result = greet("Alice")
print(result)
Hello, Alice
""
Can a function return multiple values in Python? If so, how are they typically returned?
What does the return statement do to the execution flow of a function?
Consider the function: def check_number(num): if num > 10: return "Large" elif num > 5: return "Medium" else: return "Small" What is the value of check_number(7)?
def check_number(num):
if num > 10:
return "Large"
elif num > 5:
return "Medium"
else:
return "Small"
check_number(7)
"Large"
"Medium"
"Small"
Why is it generally better to use return to get a result from a function rather than printing it inside the function?
What value is returned by return without any expression after it (i.e., just return)?
0
True
If you have result = calculate_area(10, 5), what does this imply about the calculate_area function?
result = calculate_area(10, 5)
calculate_area
length
width
Your Score: 0 / 10