[Tutor] functions in Python
Eric Walker
ewalker at micron.com
Tue Apr 18 14:46:07 CEST 2006
Payal Rathod wrote:
>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
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
New at this but the f(x) with the return statement passes the value back
to be used in something. The one with the print statement just prints
it. Correct me if I am wrong experts
def f(x):
x = x + 1;
return x
def g(x):
x=x + 1;
print x;
>>> bob = 5 + f(5)
>>> print bob
bob =Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' 5 + g(5)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ewalker.vcf
Type: text/x-vcard
Size: 176 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20060418/a343e809/attachment-0001.vcf
More information about the Tutor
mailing list