|
21 | 21 | "# Write Your Own Functions"
|
22 | 22 | ]
|
23 | 23 | },
|
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "In the past notebooks, we have met (and used) several of the many functions that are shipped with Python." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "It is now time to empower you with the capability to create your own function. But what is a function?" |
| 36 | + ] |
| 37 | + }, |
24 | 38 | {
|
25 | 39 | "cell_type": "markdown",
|
26 | 40 | "metadata": {},
|
27 | 41 | "source": [
|
28 | 42 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
29 | 43 | "\n",
|
30 |
| - "A key concept." |
| 44 | + "A **function** is a named sequence of statements." |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "## Function declaration" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "If you want to create your own function:\n", |
| 59 | + "\n", |
| 60 | + "1. You first need to think at a meaningful name to give to it. \n", |
| 61 | + "2. You put the selected name after the `def` keyword, followed by curved parentheses and ending with a `:`.\n", |
| 62 | + "3. Since the following line, you indent the body of the function (that is, the code that has to be executed)." |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 1, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "def print_temp_list():\n", |
| 72 | + " temp_list = [23.0, 19.2, 18.5, 21.3, 20.4]\n", |
| 73 | + " print(temp_list)" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "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:" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 2, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [ |
| 88 | + { |
| 89 | + "name": "stdout", |
| 90 | + "output_type": "stream", |
| 91 | + "text": [ |
| 92 | + "[23.0, 19.2, 18.5, 21.3, 20.4]\n" |
| 93 | + ] |
| 94 | + } |
| 95 | + ], |
| 96 | + "source": [ |
| 97 | + "print_temp_list()" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
| 105 | + "\n", |
| 106 | + "A function has to be first declared using the `def` keyword, than it can be called (i.e., executed) multiple times." |
31 | 107 | ]
|
32 | 108 | },
|
33 | 109 | {
|
|
40 | 116 | {
|
41 | 117 | "cell_type": "markdown",
|
42 | 118 | "metadata": {
|
43 |
| - "solution2": "hidden", |
| 119 | + "solution2": "shown", |
44 | 120 | "solution2_first": true
|
45 | 121 | },
|
46 | 122 | "source": [
|
47 | 123 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
|
48 | 124 | "\n",
|
49 |
| - "Write code that ..." |
| 125 | + "Modify the code to call a function that prints the number of elements in the `sal_list`." |
50 | 126 | ]
|
51 | 127 | },
|
52 | 128 | {
|
53 | 129 | "cell_type": "code",
|
54 |
| - "execution_count": 2, |
| 130 | + "execution_count": 4, |
55 | 131 | "metadata": {
|
56 |
| - "solution2": "hidden" |
| 132 | + "solution2": "shown" |
57 | 133 | },
|
58 |
| - "outputs": [], |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "name": "stdout", |
| 137 | + "output_type": "stream", |
| 138 | + "text": [ |
| 139 | + "Nr. of elements: 7\n" |
| 140 | + ] |
| 141 | + } |
| 142 | + ], |
59 | 143 | "source": [
|
60 |
| - "x = 0" |
| 144 | + "def print_len_sal_list():\n", |
| 145 | + " sal_list = [32.6, 33.3, 34.8, 32.4, 34.3, 33.7, 32.3] # water salinity in PSU\n", |
| 146 | + " len_sal_list = len(sal_list)\n", |
| 147 | + " print(\"Nr. of elements: \" + str(len_sal_list))\n", |
| 148 | + " \n", |
| 149 | + "print_len_sal_list()" |
61 | 150 | ]
|
62 | 151 | },
|
63 | 152 | {
|
64 | 153 | "cell_type": "code",
|
65 |
| - "execution_count": null, |
| 154 | + "execution_count": 5, |
66 | 155 | "metadata": {},
|
67 | 156 | "outputs": [],
|
68 |
| - "source": [] |
| 157 | + "source": [ |
| 158 | + "sal_list = [32.6, 33.3, 34.8, 32.4, 34.3, 33.7, 32.3]" |
| 159 | + ] |
69 | 160 | },
|
70 | 161 | {
|
71 | 162 | "cell_type": "markdown",
|
|
0 commit comments