Junior — Middle
What happens to code execution after a return statement in a function?
sobes.tech AI
Answer from AI
After executing the return statement in a function, the execution of code inside that function immediately stops, and control is returned to the point where the function was called along with the specified value (if any).
This means that all instructions placed after return in the function body will not be executed.
Example:
def example():
print("Before return")
return 42
print("After return") # This code will not be executed
result = example()
print(result) # Will output: 42
In this example, the line "After return" will not be printed because the function execution is interrupted by the return statement.