Function passed as an argument returns none
Denis McMahon
denismfmcmahon at gmail.com
Wed Oct 1 20:23:59 EDT 2014
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 at gmail.com
More information about the Python-list
mailing list