I type some code like this:
def cod():
return 4
cod()
But I get no output from it. Isn't it supposed to output a "4" when I call the function "cod()" ?
I type some code like this:
def cod():
return 4
cod()
But I get no output from it. Isn't it supposed to output a "4" when I call the function "cod()" ?
When you return something in your function, you have to use it with print function, otherwise you can't display it.
def cod():
return 4
print (cod())
If you don't return something and using print, then you can use it same as in your question.
def cod():
print (4)
cod()
Note that if you use print at second one, you get this output;
>>>
4
None
>>>
That's because default return is None and you are not return something from your function, just printing it. Better using return.
return does not print any value you need to explicitly tell your function to do so
print(cod()) #python3
or
print cod() #python2
Python only automatically print the value of an expression in the interactive interpreter.
if you want to print a function value you should modify your code to be
def cod():
return 4
print cod()