|
12 | 12 | "metadata": {},
|
13 | 13 | "source": [
|
14 | 14 | "<a href=\"https://piazza.com/e-learning_python_for_ocean_mapping/summer2019/om000/home\"><img src=\"images/help.png\" alt=\"ePOM\" title=\"Ask questions on Piazza.com\" align=\"right\" width=\"10%\" alt=\"Piazza.com\\\"></a>\n",
|
15 |
| - "# Write Your Own Functions" |
| 15 | + "# Functions" |
16 | 16 | ]
|
17 | 17 | },
|
18 | 18 | {
|
|
26 | 26 | "cell_type": "markdown",
|
27 | 27 | "metadata": {},
|
28 | 28 | "source": [
|
29 |
| - "It is now time to empower you with the capability to create your own function. But what is a function?" |
| 29 | + "A function provides a mechanism to represent many lines of code targeting a specific task with a single statement." |
30 | 30 | ]
|
31 | 31 | },
|
32 | 32 | {
|
|
42 | 42 | "cell_type": "markdown",
|
43 | 43 | "metadata": {},
|
44 | 44 | "source": [
|
45 |
| - "## Function declaration" |
| 45 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
| 46 | + "\n", |
| 47 | + "A function **encapsulates** a task. " |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
| 55 | + "\n", |
| 56 | + "The variables created inside a function are **local variables**. They are not usable (**visible**) outside of the function where they are created." |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "This allows you to use variable names in your code without having to worry about whether the same names may be used inside some of the functions that you are using." |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "markdown", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
| 71 | + "\n", |
| 72 | + "Once created, a function can be used multiple times, each time that the function task is required (**code reusability**)." |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "Since reusing a function does not require to rewrite its internal code, creating functions helps to reduce duplicated code." |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "## Creating a Function: Function Declaration and Definition" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "To create your own function:\n", |
| 94 | + "\n", |
| 95 | + "1. Select a **meaningful** name for the function.\n", |
| 96 | + "2. Put the selected name after the `def` keyword, followed by curved parentheses and ending with a `:`\n", |
| 97 | + "3. Indent the code that has to be executed (the **body** of the function)." |
46 | 98 | ]
|
47 | 99 | },
|
48 | 100 | {
|
49 | 101 | "cell_type": "markdown",
|
50 | 102 | "metadata": {},
|
51 | 103 | "source": [
|
52 |
| - "If you want to create your own function:\n", |
| 104 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
53 | 105 | "\n",
|
54 |
| - "1. You first need to think of a meaningful name to give to the function. A meaningful function name reduces (and sometimes even removes) the need to [add comments in your code](003_Conditional_Execution.ipynb#Add-Comments-to-the-Code). \n", |
55 |
| - "2. You put the selected name after the `def` keyword, followed by curved parentheses and ending with a `:`.\n", |
56 |
| - "3. You indent the body of the function (that is, the code that has to be executed)." |
| 106 | + "A meaningful function name reduces (and sometimes even removes) the need to [add comments in your code](003_Conditional_Execution.ipynb#Add-Comments-to-the-Code). " |
57 | 107 | ]
|
58 | 108 | },
|
59 | 109 | {
|
|
71 | 121 | "cell_type": "markdown",
|
72 | 122 | "metadata": {},
|
73 | 123 | "source": [
|
74 |
| - "When you execute the above **Code** cell, nothing is printed. No worries, this is correct! We have **only** declared the function. We can now call the declared function:" |
| 124 | + "When you execute the above **Code** cell, nothing is printed. No worries, this is correct! We have **only** declared the function." |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "We can now use (**call**) the declared function:" |
75 | 132 | ]
|
76 | 133 | },
|
77 | 134 | {
|
|
89 | 146 | "source": [
|
90 | 147 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
91 | 148 | "\n",
|
92 |
| - "A function has to be first declared using the `def` keyword, than it can be called (i.e., executed) multiple times." |
| 149 | + "A function has to be first declared using the `def` keyword, then it can be executed multiple times. To execute a function we **call** it by typing its name followed by brackets: e.g., `my_function()`." |
93 | 150 | ]
|
94 | 151 | },
|
95 | 152 | {
|
|
102 | 159 | {
|
103 | 160 | "cell_type": "markdown",
|
104 | 161 | "metadata": {
|
105 |
| - "solution2": "shown", |
| 162 | + "solution2": "hidden", |
106 | 163 | "solution2_first": true
|
107 | 164 | },
|
108 | 165 | "source": [
|
|
115 | 172 | "cell_type": "code",
|
116 | 173 | "execution_count": null,
|
117 | 174 | "metadata": {
|
118 |
| - "solution2": "shown" |
| 175 | + "solution2": "hidden" |
119 | 176 | },
|
120 | 177 | "outputs": [],
|
121 | 178 | "source": [
|
|
154 | 211 | "cell_type": "markdown",
|
155 | 212 | "metadata": {},
|
156 | 213 | "source": [
|
157 |
| - "It is a very common requirement to be able to pass one or more variables to a function so that operations can be performed on those variables." |
| 214 | + "It is very common to **pass** one or more values to a function, so that operations can be performed on those values." |
158 | 215 | ]
|
159 | 216 | },
|
160 | 217 | {
|
|
163 | 220 | "source": [
|
164 | 221 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
165 | 222 | "\n",
|
166 |
| - "The **parameters** are used to pass the values of selected variables into a function." |
| 223 | + "The **parameters** are used to define the values that can be passed into a function." |
167 | 224 | ]
|
168 | 225 | },
|
169 | 226 | {
|
170 | 227 | "cell_type": "markdown",
|
171 | 228 | "metadata": {},
|
172 | 229 | "source": [
|
173 |
| - "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
| 230 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n", |
174 | 231 | "\n",
|
175 |
| - "The **parameters** define the **arguments** that a function can take." |
| 232 | + "When a function is called, the values passed to fulfill its parameters are called **arguments**." |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "markdown", |
| 237 | + "metadata": {}, |
| 238 | + "source": [ |
| 239 | + "We will modify the code example above to introduce the use of a parameter. " |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "markdown", |
| 244 | + "metadata": {}, |
| 245 | + "source": [ |
| 246 | + "We first add the `sal_list` argument so that you can pass *any* salinity list to the function:" |
| 247 | + ] |
| 248 | + }, |
| 249 | + { |
| 250 | + "cell_type": "code", |
| 251 | + "execution_count": null, |
| 252 | + "metadata": {}, |
| 253 | + "outputs": [], |
| 254 | + "source": [ |
| 255 | + "def print_len_sal_list(sal_list):\n", |
| 256 | + " len_sal_list = len(sal_list)\n", |
| 257 | + " print(\"Nr. of elements: \" + str(len_sal_list))" |
176 | 258 | ]
|
177 | 259 | },
|
178 | 260 | {
|
179 | 261 | "cell_type": "markdown",
|
180 | 262 | "metadata": {},
|
181 | 263 | "source": [
|
182 |
| - "By modifying the previous code example, we will add the `sal_list` parameter to the function in the previous code example. Then, we will call the modified function passing a `salinity_list` variable. " |
| 264 | + "We now create a `salinity_values` variable and pass it to the function:" |
183 | 265 | ]
|
184 | 266 | },
|
185 | 267 | {
|
|
193 | 275 | " print(\"Nr. of elements: \" + str(len_sal_list))\n",
|
194 | 276 | "\n",
|
195 | 277 | "salinity_values = [32.6, 33.3, 34.8, 32.4, 34.3, 33.7, 32.3]\n",
|
| 278 | + "\n", |
| 279 | + "# call the 'print_len_sal_list' function with the 'salinity_values' variable to fulfill the 'sal_list' parameter\n", |
196 | 280 | "print_len_sal_list(salinity_values)"
|
197 | 281 | ]
|
198 | 282 | },
|
|
202 | 286 | "source": [
|
203 | 287 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
204 | 288 | "\n",
|
205 |
| - "The name of the parameter (i.e., `sal_list`) differs from the name of the variable that was passed to the function (i.e., `salinity_values`). This makes the code **more readable** avoiding any potential confusion in the code reader about which of the lists a statement is applied to." |
| 289 | + "In the above code, the name of the `sal_list` parameter differs from the name of the `salinity_values` variable that was passed to the `print_len_sal_list` function. This makes the code **more readable**." |
206 | 290 | ]
|
207 | 291 | },
|
208 | 292 | {
|
|
323 | 407 | "cell_type": "markdown",
|
324 | 408 | "metadata": {},
|
325 | 409 | "source": [
|
326 |
| - "## Returning value of a function" |
| 410 | + "## Returning a Value from a Function" |
327 | 411 | ]
|
328 | 412 | },
|
329 | 413 | {
|
330 | 414 | "cell_type": "markdown",
|
331 | 415 | "metadata": {},
|
332 | 416 | "source": [
|
333 |
| - "By default, a function returns a special Python object: [`None`](https://docs.python.org/3.6/c-api/none.html?highlight=none#the-none-object)." |
| 417 | + "By default, a function provides back (**returns**) a special Python object: [`None`](https://docs.python.org/3.6/c-api/none.html?highlight=none#the-none-object)." |
334 | 418 | ]
|
335 | 419 | },
|
336 | 420 | {
|
|
339 | 423 | "source": [
|
340 | 424 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
341 | 425 | "\n",
|
342 |
| - "`None` denotes lack of value. It has no methods." |
| 426 | + "`None` denotes lack of value." |
343 | 427 | ]
|
344 | 428 | },
|
345 | 429 | {
|
|
368 | 452 | "cell_type": "markdown",
|
369 | 453 | "metadata": {},
|
370 | 454 | "source": [
|
371 |
| - "We will now use the returning mechanism (by means of the `return` keyword) to provide back to the function caller something potentially useful: the number of elements. " |
| 455 | + "We will now use the `return` mechanism (`return` is [a Python keyword](001_Variables_and_Types.ipynb#Python-Variable-Naming)) to provide back something potentially useful: the number of elements. " |
372 | 456 | ]
|
373 | 457 | },
|
374 | 458 | {
|
|
404 | 488 | "cell_type": "markdown",
|
405 | 489 | "metadata": {},
|
406 | 490 | "source": [
|
407 |
| - "# Summary" |
| 491 | + "## Functions and their local variables" |
| 492 | + ] |
| 493 | + }, |
| 494 | + { |
| 495 | + "cell_type": "markdown", |
| 496 | + "metadata": {}, |
| 497 | + "source": [ |
| 498 | + "As [previously stated](005_Write_Your_Own_Functions.ipynb#Functions), the **local variables** of a function are only **visible** inside the function where they are created. " |
| 499 | + ] |
| 500 | + }, |
| 501 | + { |
| 502 | + "cell_type": "markdown", |
| 503 | + "metadata": {}, |
| 504 | + "source": [ |
| 505 | + "The code below was *specifically* written to highlight some of the implications of this concept:" |
| 506 | + ] |
| 507 | + }, |
| 508 | + { |
| 509 | + "cell_type": "code", |
| 510 | + "execution_count": null, |
| 511 | + "metadata": {}, |
| 512 | + "outputs": [], |
| 513 | + "source": [ |
| 514 | + "# Define a function that calculates the flattening using the semi-major and semi-minor axes.\n", |
| 515 | + "# The function has a local variable named 'flattening'.\n", |
| 516 | + "def calc_flattening(a, b):\n", |
| 517 | + " flattening = (a - b) / a\n", |
| 518 | + " return flattening\n", |
| 519 | + " \n", |
| 520 | + "# A global variable holding a textual definition of flattening\n", |
| 521 | + "flattening = \"The measure of compression of a cirle to form an ellipse\"\n", |
| 522 | + "\n", |
| 523 | + "# Numeric values of the semi-major and semi-minor axes of an ellipse representing the shape of the Earth\n", |
| 524 | + "semi_major = 6378137.0 # meters\n", |
| 525 | + "semi_minor = 6356752.314245 # meters\n", |
| 526 | + "\n", |
| 527 | + "# Print the definition of flattening\n", |
| 528 | + "print(\"The textbook definition of flattening is: \" + flattening)\n", |
| 529 | + "\n", |
| 530 | + "# Call the previously defined function\n", |
| 531 | + "print(\"The flattening of the Earth is: \" + str(calc_flattening(semi_major, semi_minor)))\n", |
| 532 | + " \n", |
| 533 | + "# Re-print the definition of flattening \n", |
| 534 | + "# The use of the 'flattening' variable inside the function does NOT affect the value of the global 'flattening' variable!\n", |
| 535 | + "print(\"The textbook definition of flattening is still: \" + flattening)" |
| 536 | + ] |
| 537 | + }, |
| 538 | + { |
| 539 | + "cell_type": "markdown", |
| 540 | + "metadata": {}, |
| 541 | + "source": [ |
| 542 | + "The example above defines and calls a `calc_flattening` function that calculates the [flattening](https://en.wikipedia.org/wiki/Flattening) of an ellipse." |
| 543 | + ] |
| 544 | + }, |
| 545 | + { |
| 546 | + "cell_type": "markdown", |
| 547 | + "metadata": {}, |
| 548 | + "source": [ |
| 549 | + "The code has two variables with the same name:\n", |
| 550 | + "\n", |
| 551 | + "* The **local** `flattening` variable within the `calc_flattening` function.\n", |
| 552 | + "* The **global** `flattening` variable that is used to store a textual definition of the [flattening](https://en.wikipedia.org/wiki/Flattening) of an ellipse.\n", |
| 553 | + "\n", |
| 554 | + "These two variables do not affect each other. They are in a different **namespace**." |
| 555 | + ] |
| 556 | + }, |
| 557 | + { |
| 558 | + "cell_type": "markdown", |
| 559 | + "metadata": {}, |
| 560 | + "source": [ |
| 561 | + "***" |
| 562 | + ] |
| 563 | + }, |
| 564 | + { |
| 565 | + "cell_type": "markdown", |
| 566 | + "metadata": {}, |
| 567 | + "source": [ |
| 568 | + "## Summary" |
408 | 569 | ]
|
409 | 570 | },
|
410 | 571 | {
|
|
0 commit comments