Home > Net >  Why I did not get two None statements here since both x and y are void functions?
Why I did not get two None statements here since both x and y are void functions?

Time:06-14

def x():
    def y():
        print('hi there this is y function')
    y()
    print('this is function x')

print(x())

output :

hi there this is y function
this is function x
None

I was expecting None to be printed after the function x calls y since y is a void function

expected output:

 hi there this is y function
 None
 this is function x
 None



   

CodePudding user response:

Printing the return value of a function is done by your runtime environment, not by Python itself. So only the function that returns to the environment will print a value.

Many times the value None results in no printing at all.

CodePudding user response:

If you want to have your expected output, you need to print y().

def x():
    def y():
        print('hi there this is y function')
    print(y())
    print('this is function x')

print(x())

CodePudding user response:

You will not return anything, because you don't use the "return" statement. If you don't use return, you will have no return (not even None)

Furthermore you don't have to declare a function as "void".

  • Related