X-Received: by 10.43.137.2 with SMTP id im2mr1152582icc.22.1412216974129; Wed, 01 Oct 2014 19:29:34 -0700 (PDT) X-Received: by 10.50.108.114 with SMTP id hj18mr2703igb.9.1412216974031; Wed, 01 Oct 2014 19:29:34 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!uq10no2266887igb.0!news-out.google.com!rp1ni10647igb.0!nntp.google.com!uq10no2266879igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Wed, 1 Oct 2014 19:29:33 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=59.95.13.151; posting-account=mBpa7woAAAAGLEWUUKpmbxm-Quu5D8ui NNTP-Posting-Host: 59.95.13.151 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6bf1fc74-e40f-41f0-88b9-e4543c731e69@googlegroups.com> Subject: Re: Function passed as an argument returns none From: Rustom Mody Injection-Date: Thu, 02 Oct 2014 02:29:34 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.python:78574 On Thursday, October 2, 2014 4:07:44 AM UTC+5:30, Shiva wrote: > Hi, > I am learning Python (version 3.4) strings.I have a function that takes i= n a > parameter and prints it out as given below. > def donuts(count): > if count <=3D 5: > print('Number of donuts: ',count) > else: > print('Number of donuts: many') > return > It works fine if I call=20 > donuts(5) > It returns: > we have 5 DN (as expected) > However if I do : > test(donuts(4), 'Number of donuts: 4') > where test is defined as below: > def test(got, expected): > print('got: ', got, 'Expected:' ,expected) > if got =3D=3D expected: > prefix =3D ' OK ' > else: > prefix =3D ' X ' > print (('%s got: %s expected: %s') % (prefix, repr(got), repr(expected)= )) > Only 'None' gets passed on to parameter 'got' instead of the expected val= ue > of 4. > Any idea why 'None' is getting passed even though calling the donuts(4) > alone returns the expected value? Others have given you the correct python-technicalities answer:=20 print =E2=89=A0 return However given your starting point =E2=80=93 I am learning Python =E2=80=93 = let me try=20 a less technical one. You start from the assumption that your donuts returns 5, which it seems to in some contexts and not in others. So by now you know there are 2 kinds of return: >>> def foo(x): return x+5 ...=20 >>> def bar(x): print(x+5) ...=20 >>> foo(4) 9 >>> bar(4) 9 So they seem identical here, right? But as you further discover bar is useless to do anything other than being called at the command line, whereas foo can be used in all sorts of= =20 ways: in particular you can use foo to write bar: def bar(x): print(foo(x)) But not conversely. So the morals in short:=20 1. Stick to the return that works -- python's return statement -- and avoid the return that seems to work -- the print statement 2. print is good for debugging code. When your code is bugfree it should ha= ve few/no prints 3. The official tutorial/docs unfortunately foster bad habits in children b= y showing prints early -- Know better!