-1

I'm an absolute beginner to Python and programming in general and I have just had my first exposure to functions.

I have define two simple functions below:

def output1():
   print "Hello, world!"

def output2():
   print "Hello, there!"

output1()
output2()

Having saved the above to a script called function.py, I then use the windows power shell to run the script, and it prints the following as you would expect:

Hello, world!
Hello, there!

However when I amend the script to:

def output1():
   print "Hello, world!"

def output2():
   print "Hello, there!"

print output1()
print output2()

It prints:

Hello, world!
None
Hello, there!
None

Out of curiosity why does it do this when I prefix output1 and output2 with print?

seeker
  • 530
  • 1
  • 4
  • 14
  • If you are new to Python, why are you starting with Python 2? The [Stack Overflow Python community](http://sopython.com) overwhelmingly [recommends](http://sopython.com/wiki/What_tutorial_should_I_read%3F) starting with Python 3, as does [python.org itself](https://wiki.python.org/moin/Python2orPython3). Version 3 is the present and future of the language, while 2 is the past. In learning 2 first, you'll pick up many bad habits that will need to be corrected when you learn 3 (which you'll need to do eventually), so it's much better to start with 3 first, then learn the differences in 2 later. – MattDMo Oct 02 '16 at 18:17
  • I've started with 2.7 because most of the tutorials I am viewing use it. Further to this I have read that there are libraries that are incompatible with 3. I guess I can always learn 3 later, I really doubt the differences between the two are staggering anyway. @MattDMo – seeker Oct 03 '16 at 13:39
  • 1
    there are very few libraries left that don't work with 3. Depending on what type of work you'll be doing, this may never even be an issue. It was 5 years ago, but not now. You *will* learn bad habits with 2 that will have to be unlearned later, so if you're just learning now, it's much better to learn the good way first. The standard lib in 3.5 is much improved over 2.7, with more features, meaning fewer third-party imports are necessary. If you want to learn 2 first, that's your choice, but many people with a lot more experience than me very strongly recommend 3. – MattDMo Oct 03 '16 at 13:55

2 Answers2

4

A function can have a return value: you call the function and it returns something back to where it was called.

A function returns None by default in Python.

You're now also printing the return value of output1 and output2.

You can read more about this in this section of the Python language tutorial.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
2

It works like this, because:

print output1()

means that you want to print the RESULT of the function (ie. the value of return).

And because there is no return in your function, by default it's None.

def output1():
   print "Hello, world!"
def output2():
   print "Hello, there!"
   return "Something"

print output1()
print output2()

Would result in:

Hello, world!
None
Hello, there!
Something
Nf4r
  • 109
  • 3