-1

Attached picture related to return value

In the attached picture, I am wondering why there are two results? Is it due to the def function derive one and the print function derive another one? Thank you!

MorrisWong
  • 33
  • 3
  • Please include the code in the text of your question. https://stackoverflow.com/editing-help – perigon Jul 27 '17 at 04:10
  • In Python, all functions either return an object (`None` by default) or raise an exception. Printing is a side effect intended for the user. It is not the same as a returned object used by the program and the user. Design functions with purpose centered on what objects they return. – pylang Jul 27 '17 at 06:21

1 Answers1

2

By default, a function automatically returns None:

def f():
    pass

>>> print(f())
None

You can, of course, specify other return values:

def f():
    return 42

>>> print(f())
42

In your example, the function prints a value and returns None. The second print then displays the None. There are two prints -- that is why you see two values printed.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • indeed. To clarify. Your function doesn't return anything (i.e it returns None by default). Thus your print(half_value(42)) first exectus half_value(42) which prints 21.0 and then executes print on the return value of half_value which is None – user1352683 Jul 27 '17 at 04:10