[Tutor] Tutor Digest, Vol 26, Issue 55

Carroll, Barry Barry.Carroll at psc.com
Mon Apr 17 20:48:08 CEST 2006


Payal:

> -----Original Message-----
> Date: Mon, 17 Apr 2006 13:24:31 -0400
> From: Payal Rathod <payal-python at scriptkitchen.com>
> Subject: Re: [Tutor] functions in Python
> To: Steve Nelson <sanelson at gmail.com>
> Cc: "Python\[Tutor\]" <tutor at python.org>
> Message-ID: <20060417172431.GA26463 at tranquility.scriptkitchen.com>
> Content-Type: text/plain; charset=us-ascii
> 
> On Mon, Apr 17, 2006 at 05:42:05PM +0100, Steve Nelson wrote:
> > When you define a function, you are writing a block of code which
you
> > can ask to perform a task.  The task may be simple, and not require
> > any additional information, or it may be more complex and need
> > information.
> 
> What is the difference between,
> 
> >>> def f(x):
> ...     return x
> ...
> >>> f(4)
> 4
> 
> >>> def f(x):
> ...     print x
> ...
> >>> f(4)
> 4
> 
> Both give same results. So, why return statement is needed?
> With warm regards,
> -Payal

Let's use your two functions named and see what we can do with them.  In
order to use both function definitions in the same interactive session,
I will name the first 'f1' and the second 'f2'.  

>>>>>>>>>>
>>> def f1(x):
...     return x
...     
>>> def f2(x): 
...     print x
...     
>>>>>>>>>>

Okay so far?  

Now, let's use each of these functions in a simple computation.  To
start, we'll just add two integers (called literals in programming
jargon),  

>>>>>>>>>>
>>> 4 + 4
8
>>>>>>>>>>

The '+' operator takes two integers, adds them together and gives back
(returns) the result.  (Actually, the '+' operator can do more than
this, but this is enough for this example.)  

Now, we'll do the same calculation using f1 instead of the literals.   

>>>>>>>>>>
>>> f1(4) + f1(4)
8
>>>>>>>>>>

This worked as expected:  f1 sent back (returned) the value we gave it
(the argument 4) and the '+' operator did the addition.  
Questions?

Now, let's do the same calcuation once more, using f2 this time.

>>>>>>>>>>
>>> f2(4) + f2(4)
4
4
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' 
>>>>>>>>>>

This time the operation failed. The f2 function did what we wrote it to
do: it printed the argument to the Standard Output device (also called
STDOUT), the interactive shell window in this case.  But, as the error
message indicates, it also RETURNED a value: in this case None, which
has the type NoneType.  The '+' operator then tried to add the two None
values together and failed.  

The examples above show that, although the output of f1 and f2 look the
same, it is actually different.  The 'f2' function displays its argument
for humans to read.  The 'f1' function makes the argument available for
other code to use.  That is the purpose of the 'return' statement: to
make the results of the function available to other code.  

It is important to remember that, in Python, EVERY function returns a
value.  If a 'return' statement is included in the function, the value
the result of the expression associated with the statement (in your
example, 4).  If no 'return' statement is included, the function returns
None.  

I hope this makes sense to you.  If not, keep asking questions.  

Best Regards,
 
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed




More information about the Tutor mailing list