-1

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()" ?

Alex
  • 293
  • 2
  • 10

3 Answers3

1

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.

GLHF
  • 3,835
  • 10
  • 38
  • 83
1

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.

styvane
  • 59,869
  • 19
  • 150
  • 156
0

if you want to print a function value you should modify your code to be

def cod():
    return 4

print cod()