0

Our professor told us that return should always be included, BUT there are some instances that do not "require" return at all (at least that's what I think). The way I understand it is, return sends a value of whatever is in front of it (assuming it has a value; otherwise it doesn't return anything?). But I've come across some functions that function only as a printer, e.g. you have defined a function that, when called upon prints a certain element of a list, and it doesn't matter where the list is (if it's inside the function, before the function, or even after the function) as long as it is created before the line where you call the said function, and this function didn't contain a return and why would it? I mean the only purpose of this function is to print and nothing else. So, after reading what I've written so far, can you tell me if I am misunderstanding something or is return "supposed" to be there no matter what? I mean even if I included return, what would it return? I haven't seen any function (so far) that just has a return with nothing in front of it.

  • 3
    Take a look at http://stackoverflow.com/questions/15300550/python-return-return-none-and-no-return-at-all – Wondercricket Oct 14 '15 at 14:27
  • 5
    Simple answer here. Your professor said that return should always be included. Your professor is wrong. – Kevin Oct 14 '15 at 14:28
  • 2
    A return *is* always included; in the absence of an explicit `return` statement (or in the presence of an empty `return`), Python functions/methods implicitly `return None`. – jonrsharpe Oct 14 '15 at 14:29
  • @Kevin thank you, that was a clear answer to what the professor said. I think I understand now, excluding a return and including a return with None is the same thing because both return "None". Thank you all – Dick Armstrong Oct 14 '15 at 14:33
  • 1
    There are quite a few "golden rules" that teachers keep on inflicting to newbies without explanations and whatever the context. In the best case, your teacher's intent is to force you to think about what your function should return and why. In the worst case... well, bad news, not all teachers should teach. – bruno desthuilliers Oct 14 '15 at 14:36

1 Answers1

2

Lets take your example about a function printing a value

def func():
    print 'hello'

def func():
    print 'hello'
    return None

Both of these functions are technically the same as both of them return None

Check this link for function basics which makes a point about return

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

So

def func():
    print 'hello'
    return

is also the same as above two examples

sam
  • 2,033
  • 2
  • 10
  • 13