Boston Python Workshop 8/Friday/Tutorial: Difference between revisions

Content added Content deleted
imported>Jesstess
No edit summary
imported>Jesstess
No edit summary
Line 623: Line 623:


Do you understand the difference between <code>elif</code> and <code>else</code>? When do you indent? When do you use a colon? If you're not sure, talk about it with a neighbor or staff member.
Do you understand the difference between <code>elif</code> and <code>else</code>? When do you indent? When do you use a colon? If you're not sure, talk about it with a neighbor or staff member.

==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>arguments</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>

Arguments should have names that usefully describe what they are used for in the function.

<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 arguments, 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.

====What is the difference between <code>print</code> and <code>return</code>?====

Think for a moment about the differences between <code>print</code> and <code>return</code>:

* <code>print</code> prints output to the screen so your eyes can see it.

* <code>return</code> is used to hand off a value from inside a function to a variable outside the function.

For example:

<pre>
def add(x, y):
print x + y
</pre>

will print <code>x + y</code> to the screen so your eyes can see it.

<pre>
def add(x, y):
return x + y
</pre>

will hand off <code>x + y</code> from inside the function to outside the function. This allows you to do something like:

<pre>
result = add(5, 6)
print result
</pre>

Does that make sense? If not, talk about it with a neighbor or staff member.


==End of Part 2==
==End of Part 2==