1

Assuming a function has a void return type (i.e. it doesn't return a value so its return value is None), is there a difference between

def f(x): return f(x_) if not stopping

and

def f(x): f(x_) if not stopping

where stopping is some stopping condition and x_ is some input to f?

nucsit026
  • 652
  • 7
  • 16
marisbest2
  • 1,346
  • 2
  • 17
  • 30
  • 1
    There is no difference, as long as `f` always returns `None`. If a function has no explicit `return` statement, it implicitly returns `None`. – DYZ Mar 15 '17 at 21:48
  • Right so the second one is `f(x_) if not stopping; return None`. I'm wondering if the stack calls are different, although without TCE I assume not – marisbest2 Mar 15 '17 at 21:49
  • 1
    A relevant question: http://stackoverflow.com/questions/15300550/python-return-return-none-and-no-return-at-all – DYZ Mar 15 '17 at 21:49
  • 1
    Well, both. are equivalent in that they are `SyntaxErrors`. In any event, there is no concept of a return type for a Python function. Python is a dynamically typed language. They would not be equivalent depending on how you interpret what you mean. – juanpa.arrivillaga Mar 15 '17 at 21:54
  • But you need to `return` your recursive call, if you want the caller to have access to it's value. Likely, if it is `None` you don't need that, but you *might* (e.g. `None` is a base-case). – juanpa.arrivillaga Mar 15 '17 at 21:56
  • In the question I made clear that the function doesn't have a return value (or since every function in python has a return value, that value is `None`). RE syntax I just figured it'd be easier to read this way. Assume there's also an else – marisbest2 Mar 15 '17 at 22:01
  • Also, recursion is a terrible way to implement a simple loop in Python. Use a while loop. – chepner Jan 27 '20 at 12:43

1 Answers1

0

It would appear that there is no difference between the two. Both return None and the stack is the same.

marisbest2
  • 1,346
  • 2
  • 17
  • 30