Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this function return "None None" as well? [duplicate]

I've searched regarding this and came across list returning functions but I still don't understand it.

I'm trying to understand why Print function to another function returns the following :

Happy Birthday
Happy Birthday
None None

My Code:

def happy():
    print("Happy Birthday")

def main():
    print(happy(), happy())

main()

I know that function returns special object called :None. But I'm just trying to understand why it does so?

like image 615
Bain Avatar asked Jul 30 '26 18:07

Bain


2 Answers

Every function always returns a value. If you don't explicitly return a value, and the function just gets all the way to the end, then it automatically returns None. Your function happy doesn't have any return statement, so at the end of the function, it automatically returns None.

like image 191
BrenBarn Avatar answered Aug 01 '26 08:08

BrenBarn


when you call to happy() returns none, so after each function is called you are printing

print(None, None)

like image 33
Snake Sanders Avatar answered Aug 01 '26 08:08

Snake Sanders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!