[Tutor] return, why do I need it?

Robert Sjöblom robert.sjoblom at gmail.com
Sun Dec 11 19:53:54 CET 2011


> On 12/12/2011 01:38 AM, Pete O'Connell wrote:
>> Hi I have been writing python code for a while now and I never return
>> anything within any of my functions, I just (eg.) print stuff or make
>> directories or update a log or what have you. When I look at other
>> people's code they are always returning in their functions and I was
>> wondering if someone could give me an example of when I would absolutely
>> have to return something. The thing I don't like about returning is that
>> when I unindent a function and try to run the code to inspect parts of
>> it for debugging I always have to alter the code so as not to get the
>> "return not inside a function error", so I will change the word
>> "return" to "print" and in many cases that's the way I leave it. Anyone
>> have any thoughts on this?

I don't think recursion would work very well without return values. For a simple example:

def factor(a):
   if a == 0: return 1
   else: return a * factor(a-1)

This isn't a very good way to handle factorization, but it shows recursion somewhat well.


More information about the Tutor mailing list