Anonymous user
Boston Python Workshop 3/Friday/Tutorial: Difference between revisions
Boston Python Workshop 3/Friday/Tutorial (view source)
Revision as of 14:54, 16 June 2011
, 13 years ago→Writing Functions
imported>Jesstess |
imported>Jesstess |
||
Line 680:
You can return any Python object: numbers, strings, booleans ... even other functions!
Once you execute a return, you are done with the function -- you don't get to do any more work. That means if you have a function like this:
<pre>
def absoluteValue(number):
if number < 0:
return number * -1
return number
</pre>
if <code>number</code> is less than 0, you return <code>number * -1</code> and never even get to the last line of the function. However, if <code>number</code> is greater than or equal to 0, the conditional for the if block evaluates to <code>False</code>, so we skip the code in the if block and <code>return number</code>. We could have written the above function like this if we wanted. It's the same logic, just more typing:
<pre>
def absoluteValue(number):
if number < 0:
return number * -1
else:
return number
</pre>
<b>Step 4: use the function</b>
|