|
8 | 8 | "- named sequence of statements that execute together to solve some task \n", |
9 | 9 | "- primary purpose is to help us break the problem into smaller sub-problems or tasks\n", |
10 | 10 | "- two types: fruitful and void/fruitless functions\n", |
11 | | - "- must be defined before it can be used\n", |
12 | | - "- syntax for a function definition:\n", |
| 11 | + "- must be defined before it can be used or called (two step process)\n", |
| 12 | + "- syntax to define a function:\n", |
13 | 13 | "```python\n", |
14 | 14 | "def functionName( PARAMETER1, PARAMETER2, ... ):\n", |
15 | 15 | " # STATEMENTS\n", |
16 | 16 | " return VALUE\n", |
17 | 17 | "```\n", |
18 | 18 | "- PARAMETERS and return statements are OPTIONAL\n", |
19 | 19 | "- function NAME follows the same rules as a variable/identifier name\n", |
20 | | - "- some built-in functions and object methods have been used..." |
| 20 | + "- some built-in functions and object methods have been used...\n", |
| 21 | + "- syntax to call function\n", |
| 22 | + "\n", |
| 23 | + "```python\n", |
| 24 | + "VARIABLE = functionName( ARGUMENT1, ARGUMENT2, ...)\n", |
| 25 | + "```" |
21 | 26 | ] |
22 | 27 | }, |
23 | 28 | { |
|
29 | 34 | "- give you an opportunity to name a group of statements, which makes your program easier to read and debug\n", |
30 | 35 | "- can make a program smaller by eliminating repetitive code. Later, if you make a change, you only have to make it in one place\n", |
31 | 36 | "- allow you to debug the parts one at a time (in a team) and then assemble them into a working whole\n", |
32 | | - "- write once, test, share, and reuse many times (libraries, e.g.)\n" |
| 37 | + "- write once, test, share, and reuse many times (libraries, e.g.)" |
33 | 38 | ] |
34 | 39 | }, |
35 | 40 | { |
|
41 | 46 | "- call/use function" |
42 | 47 | ] |
43 | 48 | }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "## void/fruitless functions" |
| 54 | + ] |
| 55 | + }, |
44 | 56 | { |
45 | 57 | "cell_type": "code", |
46 | | - "execution_count": 21, |
| 58 | + "execution_count": 2, |
47 | 59 | "metadata": { |
48 | 60 | "scrolled": true |
49 | 61 | }, |
50 | 62 | "outputs": [], |
51 | 63 | "source": [ |
52 | 64 | "# Function definition\n", |
53 | | - "# void function; returns None by default\n", |
54 | 65 | "def greet():\n", |
55 | 66 | " print('Hello World!')" |
56 | 67 | ] |
57 | 68 | }, |
58 | 69 | { |
59 | 70 | "cell_type": "code", |
60 | | - "execution_count": 22, |
| 71 | + "execution_count": 3, |
61 | 72 | "metadata": { |
62 | 73 | "scrolled": true |
63 | 74 | }, |
|
77 | 88 | }, |
78 | 89 | { |
79 | 90 | "cell_type": "code", |
80 | | - "execution_count": 23, |
| 91 | + "execution_count": 4, |
81 | 92 | "metadata": { |
82 | 93 | "scrolled": true |
83 | 94 | }, |
|
87 | 98 | "output_type": "stream", |
88 | 99 | "text": [ |
89 | 100 | "Hello World!\n", |
90 | | - "None\n" |
| 101 | + "a = None\n" |
91 | 102 | ] |
92 | 103 | } |
93 | 104 | ], |
94 | 105 | "source": [ |
| 106 | + "# void function; returns None by default\n", |
95 | 107 | "a = greet() # returned value\n", |
96 | | - "print(a)" |
| 108 | + "print('a =', a)" |
97 | 109 | ] |
98 | 110 | }, |
99 | 111 | { |
|
165 | 177 | "cell_type": "markdown", |
166 | 178 | "metadata": {}, |
167 | 179 | "source": [ |
168 | | - "## passing data\n", |
| 180 | + "## passing data/arguments to functions\n", |
169 | 181 | "- functions are subprograms that may need external data to work with\n", |
170 | 182 | "- one can pass data to functions via parameters/arguments\n", |
171 | 183 | "- can provide default values to parameters" |
|
381 | 393 | "source": [ |
382 | 394 | "def add(num1, num2):\n", |
383 | 395 | " \"\"\"\n", |
384 | | - " takes take numeric arguments, num1 and num2\n", |
385 | | - " returns the sum of num1 and num2\n", |
| 396 | + " takes two numeric arguments: num1 and num2\n", |
| 397 | + " calculates and returns the sume of num1 and num2\n", |
386 | 398 | " \"\"\"\n", |
387 | 399 | " total = num1 + num2\n", |
388 | 400 | " return total" |
|
571 | 583 | }, |
572 | 584 | "source": [ |
573 | 585 | "## pass by value\n", |
574 | | - "- passing immutable objects (string, int, float) as arguments to functions" |
| 586 | + "- passing immutable variables (string, int, float) as arguments to functions by copying them" |
575 | 587 | ] |
576 | 588 | }, |
577 | 589 | { |
|
0 commit comments