Python 2 hour intro: Difference between revisions

no edit summary
imported>Jesstess
imported>Jesstess
No edit summary
Line 555:
 
<b>Remember that '=' is for assignment and '==' is for comparison.</b>
 
==Writing Functions==
 
[[File:Quill.png|100px]]
 
We talked a bit about functions when we introduced the <code>type()</code> function. Let's review what we know about functions:
 
* They do some useful bit of work.
* They let us re-use code without having to type it out each time.
* They take input and possibly produce output (we say they <b>return</b> a value). You can assign a variable to this output.
* You call a function by using its name followed by its <b>arguments</b> in parenthesis.
 
For example:
 
<pre>
length = len("Mississippi")
</pre>
 
Executing this code assigns the length of the string "Mississippi" to the variable <code>length</code>.
 
We can write our own functions to encapsulate bits of useful work so we can reuse them. Here's how you do it:
 
<b>Step 1: write a function signature</b>
 
A <b>function signature</b> tells you how the function will be called. It starts with the keyword <code>def</code>, which tells Python that you are defining a function. Then comes a space, the name of your function, an open parenthesis, the comma-separated input <b>parameters</b> for your function, a close parenthesis, and a colon. Here's what a function signature looks like for a function that takes no arguments:
 
<code>def myFunction():</code>
 
Here's what a function signature looks like for a function that takes one argument called <code>string</code>:
 
<code>def myFunction(string):</code>
 
And one for a function that takes two arguments:
 
<code>def myFunction(myList, myInteger):</code>
 
Parameters should have names that usefully describe what they are used for in the function.
 
We've used the words <b>parameters</b> and <b>arguments</b> seemingly interchangeably to reference the input to functions. The distinction isn't really important right now, but if you're curious: in function signatures the input is called parameters, and when you are calling the function the input is called arguments.
 
<b>Step 2: do useful work inside the function</b>
 
Underneath the function signature you do your useful work. Everything inside the function is indented, just like with if/else blocks, so Python knows that it is a part of the function.
 
You can use the variables passed into the function as parameters, just like you can use variables once you define them outside of functions.
 
<pre>
def add(x, y):
result = x + y
</pre>
 
<b>Step 3: return something</b>
 
If you want to be able to assign a variable to the output of a function, the function has to <b>return that output</b> using the <code>return</code> keyword.
 
<pre>
def add(x, y):
result = x + y
return result
</pre>
 
or, even shorter:
 
<pre>
def add(x, y):
return x + y
</pre>
 
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 <code>if</code> expression evaluates to <code>False</code>, so we skip the code in the <code>if</code> block and return <code>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>
 
Once you define a function you can use it as many times as you want:
 
<pre>
def add(x, y):
return x + y
 
result = add(1234, 5678)
print result
result = add(-1.5, .5)
print result
</pre>
 
Functions don't have to return anything, if you don't want them to. They usually return something because we usually want to be able to assign variables to their output.
 
==End of Part 2==
Anonymous user