Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78572

Re: Function passed as an argument returns none

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: Function passed as an argument returns none
Date 2014-10-02 00:23 +0000
Organization A noiseless patient Spider
Message-ID <m0i5uv$uhi$1@dont-email.me> (permalink)
References <mailman.14532.1412203048.18130.python-list@python.org>

Show all headers | View raw


On Wed, 01 Oct 2014 22:37:13 +0000, Shiva wrote:

> Hi,
> I am learning Python (version 3.4) strings.I have a function that takes
> in a parameter and prints it out as given below.
> 
> def donuts(count):
>   if count <= 5:
>     print('Number of donuts: ',count)
>   else:
>     print('Number of donuts: many') return
> 
> It works fine if I call donuts(5)
> 
> It returns:

No it doesn't

> we have 5 DN  (as expected)

It doesn't return anything, it prints something out.

Printing something out from within a function is not the same as 
returning something.

Consider the following::

def nonsense( something ):
    print( "something is", something )
    return ( 5, "elephants", { "monkey": "peanut", "baboon": 
             "banana", "numbers": ( 1, ), }, [ 1, 2, 3, 4, 5, 
             "mouse",( -6.34565e-35, 4.765213e84, ), ], 
             None, True, False, )

print( "nonsense( 10 ) is", nonsense( 10 ) )
print( "nonsense( None ) is", nonsense( None ) )
print( "nonsense( ( 5, \"donuts\", ) ) is", nonsense( ( 5, "donuts", ) ) )

The above code shows there is no automatic connection between data output 
carried out within a function and the value (if any) returned by that 
function.

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Function passed as an argument returns none Shiva <shivaji_tn@yahoo.com> - 2014-10-01 22:37 +0000
  Re: Function passed as an argument returns none Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-02 00:23 +0000
  Re: Function passed as an argument returns none Rustom Mody <rustompmody@gmail.com> - 2014-10-01 19:29 -0700
    Re: Function passed as an argument returns none Chris Angelico <rosuav@gmail.com> - 2014-10-02 14:02 +1000
      Re: Function passed as an argument returns none Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-02 17:59 +1000
        Re: Function passed as an argument returns none cl@isbd.net - 2014-10-02 10:43 +0100
          Re: Function passed as an argument returns none Rustom Mody <rustompmody@gmail.com> - 2014-10-02 04:30 -0700
        Re: Function passed as an argument returns none Rustom Mody <rustompmody@gmail.com> - 2014-10-02 04:38 -0700
          Re: Function passed as an argument returns none Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-03 10:10 +1000
            Re: Function passed as an argument returns none Rustom Mody <rustompmody@gmail.com> - 2014-10-02 18:49 -0700
              Re: Function passed as an argument returns none Chris Angelico <rosuav@gmail.com> - 2014-10-03 12:08 +1000

csiph-web