+ 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...

7th Aug 2020, 4:44 AM
Adam Haller
Adam Haller - avatar
3 ответов
+ 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.
7th Aug 2020, 4:55 AM
Maninder $ingh
Maninder $ingh - avatar
+ 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.
7th Aug 2020, 7:25 AM
Janapriyo Maitra
Janapriyo Maitra - avatar
+ 1
I understood now... Thanks guys... :D
8th Aug 2020, 5:08 AM
Adam Haller
Adam Haller - avatar