|
10 | 10 | "name": "python2" |
11 | 11 | }, |
12 | 12 | "name": "", |
13 | | - "signature": "sha256:8a8862bd7e9d15dc6e564f2d49fb18a994ecef5e2e72b91161e511e00c6545bd" |
| 13 | + "signature": "sha256:98933314083fad8f511ddedbb2e2864dc55638392d8ce23f6cb46b78f049a2a3" |
14 | 14 | }, |
15 | 15 | "nbformat": 3, |
16 | 16 | "nbformat_minor": 0, |
|
40 | 40 | }, |
41 | 41 | { |
42 | 42 | "cell_type": "heading", |
43 | | - "level": 2, |
| 43 | + "level": 1, |
44 | 44 | "metadata": {}, |
45 | 45 | "source": [ |
46 | 46 | "Functions" |
|
52 | 52 | "source": [ |
53 | 53 | "We have already seen a few functions (recall len() for calculating list and string lengths and sum() for summing lists).\n", |
54 | 54 | "\n", |
55 | | - "Python also lets us make our own functions.\n", |
56 | | - "\n", |
57 | | - "We can use functions for a number of things:\n", |
58 | | - "- We can use functions to break down a problem into parts. It makes the code more readable and is easier to understand. This is called 'program decomposition' or 'factoring'.\n", |
59 | | - "- Functions can also be used instead of using the same lines of code at different times throughout a program. We call this 'code reuse' and what it does is simply reduce duplication of code.\n", |
60 | | - "- We can also use functions for 'abstraction' or 'simplification'. Using functions allows us to hide all of the details involved and put them into one place and simply call the function to execute the lines of code." |
| 55 | + "Python also lets us make our own functions." |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "heading", |
| 60 | + "level": 2, |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "Example: Greeting Program" |
61 | 64 | ] |
62 | 65 | }, |
63 | 66 | { |
64 | 67 | "cell_type": "markdown", |
65 | 68 | "metadata": {}, |
66 | 69 | "source": [ |
67 | | - "Let's start with an example. Say we have a program that wishes someone a happy birthday:" |
| 70 | + "Say we have a simple program that prints a happy birthday greeting:" |
68 | 71 | ] |
69 | 72 | }, |
70 | 73 | { |
|
95 | 98 | "metadata": {}, |
96 | 99 | "outputs": [] |
97 | 100 | }, |
| 101 | + { |
| 102 | + "cell_type": "heading", |
| 103 | + "level": 3, |
| 104 | + "metadata": {}, |
| 105 | + "source": [ |
| 106 | + "Examine the function in detail" |
| 107 | + ] |
| 108 | + }, |
98 | 109 | { |
99 | 110 | "cell_type": "markdown", |
100 | 111 | "metadata": {}, |
101 | 112 | "source": [ |
102 | | - "The top part is called the 'header' of the function. The header contains the keyword, 'def', the name of the function, a parameter, and a colon.\n", |
| 113 | + "The top part is the *header* of the function. The header contains the keyword, 'def', the name of the function, a parameter, and a colon.\n", |
103 | 114 | "\n", |
104 | | - "The keyword 'def' is short for 'define', and it's how we let Python know that we are creating, that is, defining our new function. \n", |
| 115 | + "`def` is short for *define*. It's the Python keyword used to indicate the start of a function definition.\n", |
105 | 116 | "\n", |
106 | | - "The name of our function is 'happy_birthday_carol'. It has 0 parameters. \n", |
| 117 | + "`happy_birthday_carol` is the name of the function.\n", |
107 | 118 | "\n", |
108 | | - "The body of our function contains one print line. We need to indent this line, and any other lines of code in a function's body, by 4 spaces.\n", |
| 119 | + "This is a function with no parameters.\n", |
109 | 120 | "\n", |
| 121 | + "The body of our function contains one print line. We need to indent this line, and any other lines of code in a function's body, by 4 spaces." |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "heading", |
| 126 | + "level": 3, |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "Let's call the function" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
110 | 136 | "Notice that nothing was printed out after we defined our function. To use our function we need to call it, like this:" |
111 | 137 | ] |
112 | 138 | }, |
|
331 | 357 | "Note that we have a 'cupcakes' parameter as well as a 'guests' parameter. Our function is called and passes in two integer values of 4 and 15. It then prints out the returned result, 60." |
332 | 358 | ] |
333 | 359 | }, |
| 360 | + { |
| 361 | + "cell_type": "heading", |
| 362 | + "level": 2, |
| 363 | + "metadata": {}, |
| 364 | + "source": [ |
| 365 | + "What are functions used for?" |
| 366 | + ] |
| 367 | + }, |
334 | 368 | { |
335 | 369 | "cell_type": "heading", |
336 | 370 | "level": 3, |
337 | 371 | "metadata": {}, |
| 372 | + "source": [ |
| 373 | + "Program decomposition, or factoring" |
| 374 | + ] |
| 375 | + }, |
| 376 | + { |
| 377 | + "cell_type": "markdown", |
| 378 | + "metadata": {}, |
| 379 | + "source": [ |
| 380 | + "To break down a problem into parts. It makes the code more readable and is easier to understand." |
| 381 | + ] |
| 382 | + }, |
| 383 | + { |
| 384 | + "cell_type": "heading", |
| 385 | + "level": 3, |
| 386 | + "metadata": {}, |
| 387 | + "source": [ |
| 388 | + "Code reuse" |
| 389 | + ] |
| 390 | + }, |
| 391 | + { |
| 392 | + "cell_type": "markdown", |
| 393 | + "metadata": {}, |
| 394 | + "source": [ |
| 395 | + "Functions can be used instead of repeating the same lines of code at different times throughout a program. This reduces duplication of code." |
| 396 | + ] |
| 397 | + }, |
| 398 | + { |
| 399 | + "cell_type": "heading", |
| 400 | + "level": 3, |
| 401 | + "metadata": {}, |
| 402 | + "source": [ |
| 403 | + "Abstraction or simplification" |
| 404 | + ] |
| 405 | + }, |
| 406 | + { |
| 407 | + "cell_type": "markdown", |
| 408 | + "metadata": {}, |
| 409 | + "source": [ |
| 410 | + "Using functions allows us to hide all of the details involved and put them into one place and simply call the function to execute the lines of code." |
| 411 | + ] |
| 412 | + }, |
| 413 | + { |
| 414 | + "cell_type": "heading", |
| 415 | + "level": 2, |
| 416 | + "metadata": {}, |
338 | 417 | "source": [ |
339 | 418 | "Exercise: Why are functions useful?" |
340 | 419 | ] |
|
0 commit comments