1 00:00:00,000 --> 00:00:02,000 Python functions, 2 00:00:02,000 --> 00:00:04,000 these are really important to know. 3 00:00:04,000 --> 00:00:07,800 So most programming languages have this thing called a function, 4 00:00:08,000 --> 00:00:10,910 and we've actually used these a few times, such as 'print', 5 00:00:10,940 --> 00:00:13,260 we've used 'type', we've used 'input', 6 00:00:13,270 --> 00:00:14,910 we've used 'enumerate'. 7 00:00:14,920 --> 00:00:18,610 These are all functions, but these ones all come built-in 8 00:00:18,610 --> 00:00:19,600 with Python. 9 00:00:19,600 --> 00:00:24,000 We can also create our own functions and name them anything we want. 10 00:00:24,000 --> 00:00:27,210 Now, a function is really just a piece of code that takes 11 00:00:27,220 --> 00:00:30,350 a variable and, I don't know, performs some sort of action, 12 00:00:30,380 --> 00:00:33,320 does some sort of logic, and spits out an answer. 13 00:00:33,400 --> 00:00:37,400 So if there was a Python function that was called 'add_two_numbers', 14 00:00:37,400 --> 00:00:41,400 and you put 1 and 45 in there, you would expect 15 00:00:41,400 --> 00:00:44,900 this to then return the number 46. 16 00:00:44,980 --> 00:00:47,860 Well, we're going to learn how to basically create this because 17 00:00:47,870 --> 00:00:51,440 this is not a Python built-in function, but we can create 18 00:00:51,440 --> 00:00:54,000 something like this, and we can make them super complex, 19 00:00:54,000 --> 00:00:55,400 or we can keep them super simple. 20 00:00:55,500 --> 00:00:57,700 But first things first, let's go ahead and take a look at 21 00:00:57,700 --> 00:00:58,900 some of these syntax. 22 00:00:58,900 --> 00:01:01,500 Whenever we create a new function, it always starts with 23 00:01:01,600 --> 00:01:04,700 the three letters 'd-e-f', 'def'. 24 00:01:04,700 --> 00:01:08,800 So we're going to define a new function, and we can call it 25 00:01:08,800 --> 00:01:12,000 'func_name', and we can have parameters in here. 26 00:01:12,040 --> 00:01:14,910 So just as an example, let's keep this simple, and let's not 27 00:01:14,920 --> 00:01:16,080 have any parameters in here. 28 00:01:16,080 --> 00:01:19,700 So we have a function here called 'func_name', and it's supposed to 29 00:01:19,700 --> 00:01:21,900 do something, so 'print("Hello")'. 30 00:01:21,900 --> 00:01:25,300 And now to execute this 'func_name()', we just type 'func_name()', 31 00:01:25,300 --> 00:01:27,300 [no audio] 32 00:01:27,300 --> 00:01:28,400 and it prints out "Hello". 33 00:01:29,200 --> 00:01:33,900 And that's literally all there is to a function, and 34 00:01:33,900 --> 00:01:35,300 we can make this more complex. 35 00:01:35,300 --> 00:01:36,500 We can add more logic in here. 36 00:01:36,580 --> 00:01:39,540 There are some extra caveats like scoping, but we'll talk 37 00:01:39,550 --> 00:01:40,410 about that down the road. 38 00:01:40,600 --> 00:01:45,500 But essentially you just created a function very similar to 'print'. 39 00:01:45,500 --> 00:01:48,400 Now we can create another function, and let's actually create that 40 00:01:48,400 --> 00:01:51,000 'add_two_numbers' function, 41 00:01:51,020 --> 00:01:53,520 and this thing can have a parameter. 42 00:01:53,700 --> 00:01:57,150 So let's say 'num1' is going to be the first parameter, 43 00:01:57,160 --> 00:01:59,730 it's going to be the only parameter right now, and it's going to 44 00:01:59,740 --> 00:02:01,800 'print(answr)', whatever 45 00:02:01,800 --> 00:02:03,300 that's going to be, the 'answer'. 46 00:02:04,100 --> 00:02:06,300 And I'm going to create a variable in here called 'answer', 47 00:02:06,300 --> 00:02:08,100 and that's going to be 'num1 +', 48 00:02:08,120 --> 00:02:09,080 and then just a number 49 00:02:09,080 --> 00:02:12,100 I'm going to tell it to add, so 3.14. 50 00:02:13,500 --> 00:02:16,900 And then we can say 'add_two_numbers', 51 00:02:18,400 --> 00:02:20,000 and then we can put a number in here. 52 00:02:20,030 --> 00:02:23,240 Now this is called an argument or a parameter. 53 00:02:23,720 --> 00:02:26,930 And all we're doing is saying, "Hey, in this particular function, 54 00:02:26,930 --> 00:02:31,300 let's throw in some extra data", and then we can use this 55 00:02:31,300 --> 00:02:33,870 exact variable name inside of this function. 56 00:02:34,500 --> 00:02:37,600 So let's go ahead, and just say 80. 57 00:02:37,640 --> 00:02:40,070 Now this is going to run 'add_two_numbers' together. 58 00:02:40,080 --> 00:02:43,160 This is effectively the number 80, and the 'answer' is going 59 00:02:43,170 --> 00:02:44,780 to be 80 + 3.14, 60 00:02:44,790 --> 00:02:46,730 and it's going to print that 'answer' out to us. 61 00:02:47,210 --> 00:02:52,610 So should this work, this will say 83.14, just like that. 62 00:02:53,400 --> 00:02:56,600 And that is effectively an argument or a parameter. 63 00:02:56,610 --> 00:02:59,540 Now, if we wanted to, we could add two parameters in here. 64 00:02:59,550 --> 00:03:03,020 And I'm going to keep this as the first example, and use a different 65 00:03:03,020 --> 00:03:04,300 cell. 'num1', 66 00:03:04,500 --> 00:03:07,900 and let's go ahead and add a second parameter, 'num2'. 67 00:03:08,500 --> 00:03:11,700 Let's simply say the 'answer = num1 + num2', 68 00:03:11,700 --> 00:03:13,800 and this is actually going to add two numbers together, 69 00:03:13,800 --> 00:03:15,000 two custom numbers. 70 00:03:15,000 --> 00:03:17,000 [no audio] 71 00:03:17,000 --> 00:03:23,400 So 'add_two_numbers'. What numbers do we want to add? Say, 42 and 24 72 00:03:25,100 --> 00:03:26,400 Sure enough, we got 66. 73 00:03:26,400 --> 00:03:27,800 It added our numbers together. 74 00:03:28,800 --> 00:03:33,300 This is great and all, but there's a problem. 75 00:03:33,300 --> 00:03:36,300 We actually can't get 'answer' outside of the function anymore. 76 00:03:36,340 --> 00:03:39,760 So if I type in 'answer', simply does not exist. 77 00:03:39,770 --> 00:03:41,050 This is a scoping problem. 78 00:03:41,060 --> 00:03:44,020 We'll talk about scoping in a future lesson, but essentially 79 00:03:44,020 --> 00:03:46,500 we created a variable here that we don't have access to. 80 00:03:46,500 --> 00:03:51,500 And let's say we wanted to store this number 66 or 83.14 81 00:03:51,500 --> 00:03:54,500 in a new variable, and work with that down the road, 82 00:03:54,520 --> 00:03:57,520 how do we store this in a variable? 83 00:03:58,900 --> 00:04:02,600 Well, up until now, we would simply put it in a variable. 84 00:04:02,660 --> 00:04:06,610 We would say the 'total = add_two_numbers', 85 00:04:06,620 --> 00:04:08,230 and I'll just move that up. 86 00:04:08,230 --> 00:04:11,800 'add_two_numbers', and let's add 12 and 34. 87 00:04:13,400 --> 00:04:18,000 Okay. That printed out the total of 46, but the total value 88 00:04:18,000 --> 00:04:21,399 of this particular variable is not 46. 89 00:04:21,399 --> 00:04:22,399 It's not what you expect. 90 00:04:22,399 --> 00:04:23,800 It's actually nothing right now. 91 00:04:24,000 --> 00:04:26,800 So let's do 'type(total)', and we're going to see this is a 92 00:04:26,800 --> 00:04:28,800 'NoneType' as expected. 93 00:04:28,820 --> 00:04:31,630 So something to keep in mind with a function is, 94 00:04:31,800 --> 00:04:36,600 a function always comes with this idea of returning an 'answer'. 95 00:04:36,600 --> 00:04:40,300 So you give it some data to work with, and it returns an 96 00:04:40,360 --> 00:04:42,660 answer to you, much like a calculator. 97 00:04:42,660 --> 00:04:48,200 So you punch in 90 divided by 3, and at the end it just says 30. 98 00:04:48,270 --> 00:04:53,860 Now, we do that with this keyword 'return', and then we could 99 00:04:53,890 --> 00:04:57,130 return anything we want in here, "anything we want in here". 100 00:04:57,140 --> 00:05:00,220 So let's take an example of this 'return' keyword. 101 00:05:00,220 --> 00:05:01,300 Let's see how this works. 102 00:05:02,200 --> 00:05:04,800 So let's create a new function in here, and let's call it 103 00:05:04,800 --> 00:05:07,600 'get_name', and it's not going to take any parameters, 104 00:05:07,600 --> 00:05:10,400 and all it's going to do is 'return "Kalob"'. 105 00:05:10,960 --> 00:05:14,200 Now, if I do 'get_name()' with the parentheses because it's a 106 00:05:14,210 --> 00:05:18,450 function, okay, it returned 'Kalob' as a string. 107 00:05:18,450 --> 00:05:19,400 So that's good. 108 00:05:19,400 --> 00:05:21,800 But I don't have a variable to work with here. 109 00:05:21,800 --> 00:05:24,600 So if I wanted to store this in a variable, I could say 'name 110 00:05:24,600 --> 00:05:25,900 = get_name', 111 00:05:26,800 --> 00:05:28,700 and then we could 'print(name)'. 112 00:05:29,800 --> 00:05:32,700 We could 'type(name)', which is going to be a string, because 113 00:05:32,760 --> 00:05:34,580 that's what we told it to return 114 00:05:34,590 --> 00:05:36,380 here. Quotations means it's a string. 115 00:05:37,500 --> 00:05:41,000 And now we're able to store function logic in a variable 116 00:05:41,000 --> 00:05:42,500 using the 'return' keyword. 117 00:05:42,520 --> 00:05:47,080 Now, let's go ahead and create a custom greeting function. 118 00:05:47,090 --> 00:05:52,390 So let's do 'def greeting()', and it's going to take a 'name', 119 00:05:52,500 --> 00:05:55,500 and this is simply going to return an f-string. 120 00:05:55,500 --> 00:05:59,300 "Welcome to Python for Everybody", 121 00:05:59,900 --> 00:06:00,900 and then the ''. 122 00:06:00,900 --> 00:06:02,900 [no audio] 123 00:06:02,900 --> 00:06:05,000 And if we run 'greeting' with a 'name' in here, 124 00:06:05,700 --> 00:06:06,700 'Henry'. 125 00:06:08,100 --> 00:06:11,000 Okay, cool. But that's still not in any sort of variable. 126 00:06:11,060 --> 00:06:14,090 So we could now assign this to a variable saying, I don't 127 00:06:14,100 --> 00:06:16,400 know, the 'sentence' or the welcome sentence is going to be 128 00:06:16,500 --> 00:06:22,100 'greeting('henry')', and let's make that capital 'H', because it's a name, 129 00:06:23,800 --> 00:06:25,300 and then we can use this later. 130 00:06:25,300 --> 00:06:27,300 So 'sentence'. 131 00:06:27,300 --> 00:06:28,800 And why is this important? 132 00:06:28,800 --> 00:06:31,400 Because now we can do things like 'sentence.', 133 00:06:31,400 --> 00:06:34,800 we can 'count' the number of times there's an "o" in here. 134 00:06:35,700 --> 00:06:37,500 There's five "o" in the sentence. 135 00:06:37,800 --> 00:06:40,800 We can do things like 'sentence.upper'. 136 00:06:40,800 --> 00:06:44,300 And so effectively we've performed some sort of logic in 137 00:06:44,300 --> 00:06:45,580 this function called 'greeting()'. 138 00:06:45,590 --> 00:06:48,460 And then with the 'return' keyword, we were able to store that 139 00:06:48,460 --> 00:06:52,100 in a variable, and now we can do anything else with that variable. 140 00:06:52,100 --> 00:06:55,100 Now, whenever you see this 'return' keyword, it is a lot like 141 00:06:55,100 --> 00:06:56,700 the 'break' keyword in a loop, 142 00:06:56,760 --> 00:07:00,150 any code below it is not going to execute, and it's going to 143 00:07:00,160 --> 00:07:03,210 immediately exit the function, just like 'break' immediately 144 00:07:03,220 --> 00:07:07,780 exit a for loop or a while loop, really, any sort of loop. 145 00:07:07,800 --> 00:07:13,300 So just as an example in here, let's do 'print("WILL THIS RUN?")'. 146 00:07:13,300 --> 00:07:15,940 Because we've noticed that the 'print' statement up here, they 147 00:07:15,950 --> 00:07:17,760 were they, were working perfectly fine. 148 00:07:17,760 --> 00:07:20,600 As soon as we called 'add_two_numbers', it would 149 00:07:20,600 --> 00:07:22,400 print the 'answer' just like this. 150 00:07:22,440 --> 00:07:28,130 So if we run 'greeting()' here, this should say "Welcome to Python 151 00:07:28,140 --> 00:07:29,690 for Everybody", whatever the 'name' is. 152 00:07:29,700 --> 00:07:31,940 So, "Henry". And ,will this run? 153 00:07:32,500 --> 00:07:33,500 We don't know. 154 00:07:33,500 --> 00:07:36,400 So let's go ahead and run this, and run that, and it does not 155 00:07:36,450 --> 00:07:39,260 run. And that's because effectively, Python says there's 156 00:07:39,270 --> 00:07:42,410 a function in here, and it hits a keyword called 'return', and 157 00:07:42,410 --> 00:07:45,500 it essentially thinks that it simply does not exist. 158 00:07:45,500 --> 00:07:49,900 Python does not care that there's anything below this 'return' keyword. 159 00:07:49,900 --> 00:07:52,200 [no audio] 160 00:07:52,200 --> 00:07:54,400 Now, the reason we want to use functions is because 161 00:07:54,470 --> 00:07:56,740 if we have any sort of logic that we're planning over and over 162 00:07:56,750 --> 00:07:59,410 again, and we wanted to be able to change over time, 163 00:07:59,420 --> 00:08:02,380 and change throughout our entire application, we can change 164 00:08:02,380 --> 00:08:03,600 it in just one spot. 165 00:08:03,660 --> 00:08:08,720 So let's create a function in here called 'can_drink', 166 00:08:08,720 --> 00:08:10,400 and it's going to take an 'age', 167 00:08:10,400 --> 00:08:14,930 and we're simply going to say 'if age > 168 00:08:14,930 --> 00:08:19,500 18: return True 169 00:08:19,700 --> 00:08:23,100 else return False. 170 00:08:23,100 --> 00:08:27,300 Now, where I'm from, if you're 18 or older, you can legally drink alcohol, 171 00:08:27,500 --> 00:08:33,500 and this works for lots of places across the globe, but not every place. 172 00:08:33,500 --> 00:08:39,400 So we could say, 'can_kalob_drink = can_drink()', 173 00:08:39,400 --> 00:08:44,299 and then I'll put my age of 30 in here, and 'can_kalob_drink' 174 00:08:45,100 --> 00:08:46,200 is going to be True. 175 00:08:46,200 --> 00:08:48,799 30 is in fact, greater than 18. 176 00:08:48,809 --> 00:08:51,799 So really all this did was say, "Put 30 in here. 177 00:08:53,000 --> 00:08:54,000 Put 30 in here. 178 00:08:54,080 --> 00:08:56,150 'if 30 > 18' 179 00:08:56,159 --> 00:08:57,140 Yes, of course it is. 180 00:08:57,140 --> 00:08:58,200 So 'return True'. 181 00:08:58,280 --> 00:09:04,850 If this was False, if this was a lower number, this would 182 00:09:04,860 --> 00:09:07,700 not be executed and it would run the 'else' statement, and then 183 00:09:07,710 --> 00:09:09,930 'return False' would be the answer. 184 00:09:09,940 --> 00:09:13,610 So we can actually take an example of this, and let's say, 185 00:09:13,670 --> 00:09:17,840 15, and I can no longer drink. 186 00:09:17,850 --> 00:09:20,330 Now, the reason you might want to do something like this 187 00:09:20,340 --> 00:09:23,490 is because in your application you might want to make it 188 00:09:23,490 --> 00:09:25,900 available to lots of people across the world. 189 00:09:25,900 --> 00:09:29,380 But in the United States, you have to be 21 or older to drink, 190 00:09:29,600 --> 00:09:32,700 and actually this should be '>='. 191 00:09:32,720 --> 00:09:36,830 But in the United States, you need to be 21 or older to drink. 192 00:09:36,840 --> 00:09:41,230 So what happens if we constantly wrote in our application, 193 00:09:41,240 --> 00:09:46,420 'if age >=18', do a thing, and then 194 00:09:46,430 --> 00:09:50,500 somewhere down the line, let's say 300 lines of code later, 195 00:09:50,500 --> 00:09:53,800 we have it again, 'if age >= 18', 196 00:09:55,200 --> 00:09:56,200 do some more stuff. 197 00:09:56,200 --> 00:09:58,200 [no audio] 198 00:09:58,200 --> 00:10:01,400 We could also do somewhere down the line again, maybe even 199 00:10:01,400 --> 00:10:10,100 in a different file, 'if age < 18', "#cannot drink". Right. 200 00:10:10,130 --> 00:10:12,790 So we have all these different use cases where we are using 201 00:10:12,800 --> 00:10:15,070 the same logic over and over and over again. 202 00:10:15,080 --> 00:10:17,380 What we can do instead is put this into a function. 203 00:10:18,600 --> 00:10:26,500 And so we can say 'if can_drink()', and then the user's 'age' is, 204 00:10:26,500 --> 00:10:27,300 I don't know, 30. 205 00:10:27,340 --> 00:10:29,590 And we could do it again here. 206 00:10:29,600 --> 00:10:31,500 So I'm just going to copy this. 207 00:10:31,500 --> 00:10:34,500 [no audio] 208 00:10:34,500 --> 00:10:40,000 And because this one is less than, we can say, 'if not', because 209 00:10:40,000 --> 00:10:42,300 that's going to 'return False', cannot drink. 210 00:10:42,340 --> 00:10:45,870 So now we're actually cleaning up our logic because down 211 00:10:45,870 --> 00:10:48,900 the road you can simply change this to 21, 212 00:10:48,920 --> 00:10:51,610 and now it affects all three of these areas. 213 00:10:51,610 --> 00:10:53,100 You don't have to change 214 00:10:53,100 --> 00:10:57,900 [no audio] 215 00:10:57,900 --> 00:11:02,300 18 to 21, to 21, to 21, you don't have to do that. 216 00:11:02,320 --> 00:11:03,480 You can just change it one spot. 217 00:11:04,800 --> 00:11:06,200 Now that's the power behind a function. 218 00:11:06,500 --> 00:11:09,230 We can even get more clever with this and assign it to a 219 00:11:09,240 --> 00:11:12,240 variable and only ever reference that variable instead of 220 00:11:12,250 --> 00:11:14,460 referencing the function over and over and over again. 221 00:11:14,580 --> 00:11:16,230 But that's a little bit boring. 222 00:11:16,240 --> 00:11:19,470 So let's talk about more parameters, more arguments. 223 00:11:19,480 --> 00:11:21,900 Let's look at a 'default' value. 224 00:11:21,900 --> 00:11:24,900 So 'def greeting()', 225 00:11:24,900 --> 00:11:29,700 and let's say we are asking for a 'name'. 'print("Hello,"', and 226 00:11:29,700 --> 00:11:34,000 then the 'name'. We can run 'greeting('Kalob')', 227 00:11:34,000 --> 00:11:35,100 and it's going to say, "Hello, Kalob". 228 00:11:35,300 --> 00:11:38,500 But what if we didn't actually pass in any argument? 229 00:11:39,900 --> 00:11:44,000 We get an error "greeting() missing 1 required positional argument: 'name'". 230 00:11:44,040 --> 00:11:46,220 So it's saying that this 'name' variable is actually missing. 231 00:11:46,220 --> 00:11:48,500 This parameter, this argument is missing. 232 00:11:48,550 --> 00:11:50,940 What we can do instead, is give it a 'default'. 233 00:11:51,000 --> 00:11:53,800 So the default is going to be "Zephyr" in this case. 234 00:11:54,040 --> 00:11:57,310 So if there is no 'name', it's going to default to "Zephyr". 235 00:11:57,320 --> 00:11:59,980 So let's run this cell, and run that cell, and it says, "Hello 236 00:11:59,990 --> 00:12:03,280 Zephyr", but if I specifically overwrite that by giving it 237 00:12:03,290 --> 00:12:08,100 a specific value in here, I can overwrite the default value. 238 00:12:08,100 --> 00:12:13,100 So 'name' is always going to be "Zephyr" by default unless you tell 239 00:12:13,100 --> 00:12:14,100 it otherwise. 240 00:12:14,900 --> 00:12:18,100 In which case this says, "Hello Henry", because we have overwritten it. 241 00:12:18,800 --> 00:12:22,400 So why or how is this even useful? 242 00:12:22,400 --> 00:12:26,210 Let's go ahead and create a new function in here, and it's 243 00:12:26,220 --> 00:12:27,920 going to be called 'create_person', 244 00:12:27,980 --> 00:12:31,010 and what this is going to do is return a dictionary of data 245 00:12:31,010 --> 00:12:35,600 for us. So we could say it needs a 'name', needs an 'age', 246 00:12:35,600 --> 00:12:37,600 [no audio] 247 00:12:37,600 --> 00:12:39,800 The 'role' might be a 'Student'. 248 00:12:39,800 --> 00:12:42,800 Maybe this is for a learning platform, 249 00:12:42,800 --> 00:12:46,500 and so every 'role' by default is going to be a 'Student'. 250 00:12:46,500 --> 00:12:50,250 And let's also pass in the required number of books that 251 00:12:50,260 --> 00:12:51,510 every student needs to have. 252 00:12:51,510 --> 00:12:53,400 And let's say the required number of books, 'required_books' 253 00:12:53,400 --> 00:12:55,200 is going to be 5 by default. 254 00:12:55,900 --> 00:12:59,900 What we can do here now, is we can create a 'person = 255 00:12:59,900 --> 00:13:04,200 a dictionary, and we can say the 'name' is going to be whatever 256 00:13:04,200 --> 00:13:05,500 this 'name' value is; 257 00:13:07,200 --> 00:13:11,800 the 'age' is going to be whatever that 'age' value is from that 'age' variable; 258 00:13:13,100 --> 00:13:15,800 the 'role' is going to be whatever 'role' is, 259 00:13:15,800 --> 00:13:17,600 and by default, that's always going to be 'Student', 260 00:13:17,600 --> 00:13:21,700 so that's always going to be filled out; and 'required_books' 261 00:13:22,900 --> 00:13:24,900 is going to be whatever 'required_books' is. 262 00:13:24,940 --> 00:13:27,600 Now, these keys in here just happen to be the same name, 263 00:13:27,600 --> 00:13:29,200 they don't have to be the same name. 264 00:13:29,200 --> 00:13:32,600 So we could say 'person_name' just as an example, 265 00:13:32,610 --> 00:13:36,710 and 'total_books = required_books', something like 266 00:13:36,710 --> 00:13:38,800 that. And then we could return this entire dictionary. 267 00:13:38,800 --> 00:13:42,100 [no audio] 268 00:13:42,100 --> 00:13:46,200 So now I can create a 'person' called 'kalob'. 'create_person()'. 269 00:13:46,280 --> 00:13:48,110 My 'name' is going to be 'Kalob', 270 00:13:48,170 --> 00:13:49,550 my 'age' is going to be 30, 271 00:13:49,550 --> 00:13:52,800 my 'role' is going to be, I'm going to overwrite this, 'Teacher', 272 00:13:52,800 --> 00:13:55,200 but I'm going to keep 'required_books = 5'. 273 00:13:55,270 --> 00:13:57,070 So 'total_books' is going to say 5. 274 00:13:57,370 --> 00:14:00,430 And when I print out 'kalob', you can see 'person_name', 275 00:14:00,460 --> 00:14:02,870 that's the key here, matches 'Kalob', 276 00:14:03,100 --> 00:14:05,100 that's here; the 'age' is 30; 277 00:14:05,100 --> 00:14:06,100 the 'role' is 'Teacher'. 278 00:14:06,200 --> 00:14:09,360 We actually overwrote the default value for 'role'. 279 00:14:09,900 --> 00:14:12,630 And fun fact, we use a little named argument here. 280 00:14:12,630 --> 00:14:16,200 So we made sure that it was specifically overwriting 'role'. 281 00:14:16,600 --> 00:14:19,300 And the 'total_books' is 5, which we didn't specify at all. 282 00:14:19,310 --> 00:14:22,120 The 'required_books' was not a parameter that we gave it, but 283 00:14:22,130 --> 00:14:23,740 it still used 5 as a default. 284 00:14:25,100 --> 00:14:26,100 Now how is this useful? 285 00:14:26,100 --> 00:14:30,200 Well, we can now say 'nathan = create_person( 286 00:14:31,100 --> 00:14:33,100 "Nathan", 29, 287 00:14:33,900 --> 00:14:39,300 his 'role' is a 'Student', and the 'required_books' he needs 288 00:14:39,300 --> 00:14:41,400 is actually only 2, so we can overwrite that. 289 00:14:41,400 --> 00:14:44,600 And let's take a look at what 'nathan' has. First 'name' is "Nathan"; 290 00:14:44,600 --> 00:14:46,300 'age' is 29; 'role' is a 'Student', 291 00:14:46,300 --> 00:14:48,300 so we actually didn't need that at all; 292 00:14:48,350 --> 00:14:50,910 and the 'required_books' is 2. 293 00:14:50,910 --> 00:14:51,990 So let's rerun this. 294 00:14:52,900 --> 00:14:53,900 And it didn't change. 295 00:14:55,100 --> 00:14:58,100 We got rid of 'role = 'Student', and it still just 296 00:14:58,100 --> 00:14:59,100 became the default. 297 00:15:00,100 --> 00:15:03,400 So a function essentially allows us to write all of our logic 298 00:15:03,470 --> 00:15:08,350 in here just once, and then down the road, we can call it 299 00:15:08,360 --> 00:15:12,150 with one line of code, and it will always create and always 300 00:15:12,150 --> 00:15:17,200 work with these five lines of code here, or six lines of code 301 00:15:17,200 --> 00:15:20,500 rather. Now, what I would like you to do, is create 302 00:15:20,500 --> 00:15:21,700 your own function. 303 00:15:21,700 --> 00:15:25,100 Go ahead and create a function that asks for user's age, cast 304 00:15:25,100 --> 00:15:29,800 that value as an integer, and then make the function say, 305 00:15:29,800 --> 00:15:31,600 "You are X years old". 306 00:15:31,680 --> 00:15:34,470 So as a quick little recap, you're going to want to create 307 00:15:34,500 --> 00:15:36,200 a function with 'def', 308 00:15:36,220 --> 00:15:38,860 then the function name, whatever that 'func_name' is going to 309 00:15:38,860 --> 00:15:41,400 be, it needs to take an 'age'. 310 00:15:42,200 --> 00:15:43,700 So 'x' could be the 'age', 311 00:15:43,700 --> 00:15:44,890 or you could just call it 'age', 312 00:15:45,250 --> 00:15:50,890 and then you need to 'return' or 'print' a string that says, "You 313 00:15:50,900 --> 00:15:54,400 are", and then whatever that '' is, "years old". 314 00:15:55,200 --> 00:15:57,600 Last but not least, what I would like you to do is create 315 00:15:57,650 --> 00:16:03,210 this function and then put it in a '.py' file, in a '.py' 316 00:16:03,220 --> 00:16:05,640 file, and then from your command line I want you to run 317 00:16:05,840 --> 00:16:09,410 'python yourfile.py', whatever your file is going to be called, 318 00:16:09,480 --> 00:16:12,000 and you should end up seeing your sentence in here. 319 00:16:12,000 --> 00:16:13,800 Don't forget, you need, you're going to need to call your 320 00:16:13,800 --> 00:16:16,500 function, so just because you have assigned it does not mean 321 00:16:16,570 --> 00:16:17,910 that it's actually going to run. 322 00:16:17,910 --> 00:16:20,100 You're going to need to do something like this. 323 00:16:20,100 --> 00:16:23,100 [no audio] 324 00:16:23,100 --> 00:16:25,400 So go ahead and give that a shot.