Skip to content

Commit da45732

Browse files
committed
Ch3-3
1 parent 9e81036 commit da45732

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

Ch03-3-Functions-UserDefined.ipynb

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
"- named sequence of statements that execute together to solve some task \n",
99
"- primary purpose is to help us break the problem into smaller sub-problems or tasks\n",
1010
"- 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",
1313
"```python\n",
1414
"def functionName( PARAMETER1, PARAMETER2, ... ):\n",
1515
" # STATEMENTS\n",
1616
" return VALUE\n",
1717
"```\n",
1818
"- PARAMETERS and return statements are OPTIONAL\n",
1919
"- 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+
"```"
2126
]
2227
},
2328
{
@@ -29,7 +34,7 @@
2934
"- give you an opportunity to name a group of statements, which makes your program easier to read and debug\n",
3035
"- 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",
3136
"- 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.)"
3338
]
3439
},
3540
{
@@ -41,23 +46,29 @@
4146
"- call/use function"
4247
]
4348
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"## void/fruitless functions"
54+
]
55+
},
4456
{
4557
"cell_type": "code",
46-
"execution_count": 21,
58+
"execution_count": 2,
4759
"metadata": {
4860
"scrolled": true
4961
},
5062
"outputs": [],
5163
"source": [
5264
"# Function definition\n",
53-
"# void function; returns None by default\n",
5465
"def greet():\n",
5566
" print('Hello World!')"
5667
]
5768
},
5869
{
5970
"cell_type": "code",
60-
"execution_count": 22,
71+
"execution_count": 3,
6172
"metadata": {
6273
"scrolled": true
6374
},
@@ -77,7 +88,7 @@
7788
},
7889
{
7990
"cell_type": "code",
80-
"execution_count": 23,
91+
"execution_count": 4,
8192
"metadata": {
8293
"scrolled": true
8394
},
@@ -87,13 +98,14 @@
8798
"output_type": "stream",
8899
"text": [
89100
"Hello World!\n",
90-
"None\n"
101+
"a = None\n"
91102
]
92103
}
93104
],
94105
"source": [
106+
"# void function; returns None by default\n",
95107
"a = greet() # returned value\n",
96-
"print(a)"
108+
"print('a =', a)"
97109
]
98110
},
99111
{
@@ -165,7 +177,7 @@
165177
"cell_type": "markdown",
166178
"metadata": {},
167179
"source": [
168-
"## passing data\n",
180+
"## passing data/arguments to functions\n",
169181
"- functions are subprograms that may need external data to work with\n",
170182
"- one can pass data to functions via parameters/arguments\n",
171183
"- can provide default values to parameters"
@@ -381,8 +393,8 @@
381393
"source": [
382394
"def add(num1, num2):\n",
383395
" \"\"\"\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",
386398
" \"\"\"\n",
387399
" total = num1 + num2\n",
388400
" return total"
@@ -571,7 +583,7 @@
571583
},
572584
"source": [
573585
"## 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"
575587
]
576588
},
577589
{

0 commit comments

Comments
 (0)