Final answer:
The purpose of a return statement in a function is to stop executing the function and return a value to the calling code.
Step-by-step explanation:
The purpose of a return statement in a function is to stop executing the function and return a value to the calling code.
When a return statement is encountered, the function immediately exits and control is transferred back to the place in the program where the function was called. Any code after the return statement will not be executed.
Here's an example:
def add_numbers(x, y):
total = x + y
return total
result = add_numbers(3, 5)
print(result) # Output: 8
In this example, the function add_numbers takes two parameters, adds them together, and returns the result. The returned value is then stored in the result variable and printed.