Code clarification


def showMessage(msg):
    print msg

def cube(x):
    return x * x * x   
x = cube(3)    
print x

showMessage("Computation finished.")
print showMessage("Ready.")

this is an example from( was from 15 min read on function topic.I am not able to understand when we run this code why "None" is being printed as one of the outputs.Kindly clarify the same.


None is returned because of this function:

def showMessage(msg):
    print msg

See how it doesn't have a return in it? All functions return something, if a return isn't explicitly stated then None is returned. That is fine, you don't always want or need a function to return something as in the showMessage() function, you just want it to print the input.

So why is None being printed?

print showMessage("Ready.")

This print statement is printing what is returned by the showMessage() function, which as I said above is None.

It will also print "Ready." because that is what the print statement within the showMessage() function is doing.


thanks for the explanation


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.