Boston Python Workshop 7/Friday/Tutorial: Difference between revisions
Content added Content deleted
imported>Jesstess |
imported>Jesstess |
||
Line 660: | Line 660: | ||
<b>Step 1: write a function signature</b> |
<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> |
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> |
<code>def myFunction():</code> |
||
Line 672: | Line 672: | ||
<code>def myFunction(myList, myInteger):</code> |
<code>def myFunction(myList, myInteger):</code> |
||
Arguments 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> |
<b>Step 2: do useful work inside the function</b> |
||
Line 680: | Line 678: | ||
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. |
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 |
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> |
<pre> |