+ 2
Python: return functions
Why isn't the first print statement being printed? def add_numbers(x, y): total = x + y return total print("This won't be printed") print(add_numbers(4, 5)) Output : 9 thank you...
3 Answers
+ 1
Because the return statement is running before the print statement in the function.
Whenever the return call in function then functions terminate. Means when return total run then function quit and print leave that's why it's not calling.
In simple language, the return is last stop section of function in all programming languages not in python.
+ 1
It's because once the return statement is encountered, the control will return back to the place from where the function was called.
Here your return statement is above the print statement and hence, it's returning the value. It's being executed before the print statement so the print statement is not executing here in the function.
Hope your doubt is cleared.
+ 1
I understood now... Thanks guys... :D