1 00:00:00,840 --> 00:00:03,870 Instructor: It's time to talk about functions. 2 00:00:03,870 --> 00:00:07,170 This is where things get really interesting. 3 00:00:07,170 --> 00:00:11,880 Because up until now we've seen functions, right? 4 00:00:11,880 --> 00:00:16,880 Functions where things like print, like list, Boolean, 5 00:00:17,490 --> 00:00:20,610 we even saw the input function to get the input 6 00:00:20,610 --> 00:00:22,950 of whatever the user types. 7 00:00:22,950 --> 00:00:27,510 And those allowed us to perform actions on our data types. 8 00:00:27,510 --> 00:00:29,910 But the true power comes when we can start 9 00:00:29,910 --> 00:00:32,670 creating our own functions. 10 00:00:32,670 --> 00:00:33,503 That's right. 11 00:00:33,503 --> 00:00:35,880 We're not limited to whatever Python gives us. 12 00:00:35,880 --> 00:00:38,550 We're able to create our own functions 13 00:00:38,550 --> 00:00:40,830 and use them in our programs. 14 00:00:40,830 --> 00:00:43,143 So let's learn how to do that. 15 00:00:44,160 --> 00:00:46,890 The way we create a function in Python. 16 00:00:46,890 --> 00:00:48,240 And functions, by the way, 17 00:00:48,240 --> 00:00:50,580 exist in all programming languages. 18 00:00:50,580 --> 00:00:52,770 They're very, very important. 19 00:00:52,770 --> 00:00:55,380 If we do def, 20 00:00:55,380 --> 00:00:57,900 that lets the Python interpreter know 21 00:00:57,900 --> 00:01:00,623 that we're about to define a function. 22 00:01:00,623 --> 00:01:02,730 Def is short for define. 23 00:01:02,730 --> 00:01:04,590 So the interpreter is gonna say, 24 00:01:04,590 --> 00:01:07,050 all right, they're about to define a function. 25 00:01:07,050 --> 00:01:09,510 What's the function going to be? 26 00:01:09,510 --> 00:01:11,910 Well, we can create whatever we want. 27 00:01:11,910 --> 00:01:15,450 We use the same naming case as we do with variables 28 00:01:15,450 --> 00:01:17,070 to define our functions. 29 00:01:17,070 --> 00:01:18,720 So let's create a function. 30 00:01:18,720 --> 00:01:20,523 Again, we'll call it, say_hello. 31 00:01:22,110 --> 00:01:24,090 Just a variable that I created, 32 00:01:24,090 --> 00:01:25,560 but this time it's a function 33 00:01:25,560 --> 00:01:27,693 because we use the def keyword. 34 00:01:28,530 --> 00:01:31,050 Now in here, in say_hello, 35 00:01:31,050 --> 00:01:34,290 we also add the brackets to let the interpreter know 36 00:01:34,290 --> 00:01:36,870 that this is something that we're gonna take action on, 37 00:01:36,870 --> 00:01:40,440 or this is going to perform an action on a data type. 38 00:01:40,440 --> 00:01:45,390 We use the colon and then within this block of code 39 00:01:45,390 --> 00:01:47,040 we can say, print('hellllooooo'). 40 00:01:50,130 --> 00:01:52,800 And that is a function. 41 00:01:52,800 --> 00:01:57,800 If I click run, hmm, nothing happened. 42 00:01:59,340 --> 00:02:00,363 Why is that? 43 00:02:01,440 --> 00:02:05,220 Well, we've created the say_hello function. 44 00:02:05,220 --> 00:02:08,220 We've defined it, and now it's living somewhere 45 00:02:08,220 --> 00:02:09,840 in memory on our machine. 46 00:02:09,840 --> 00:02:13,470 However, in order to use a function, remember, 47 00:02:13,470 --> 00:02:15,570 just like we use the print function, 48 00:02:15,570 --> 00:02:18,120 we have to call it with the brackets. 49 00:02:18,120 --> 00:02:22,280 So after we define a function, we say, say_hello. 50 00:02:24,240 --> 00:02:29,043 And did you notice how my repple, as soon as I said, say, 51 00:02:29,910 --> 00:02:33,180 actually gives me this, say_hello command, 52 00:02:33,180 --> 00:02:36,540 because I've created it and you see this purple box, 53 00:02:36,540 --> 00:02:39,000 which shows that it's a function, 54 00:02:39,000 --> 00:02:42,450 well, we now have it available for us to use 55 00:02:42,450 --> 00:02:46,080 just like we had the print function available for us to use. 56 00:02:46,080 --> 00:02:50,880 So if I do say_hello and run it with the brackets, 57 00:02:50,880 --> 00:02:55,880 and I click run, I get, hellllooooo, how cool is that? 58 00:02:57,510 --> 00:03:00,390 By the way, what happens if I run it without the brackets 59 00:03:00,390 --> 00:03:01,323 and I click run? 60 00:03:03,480 --> 00:03:05,070 Nothing happens. 61 00:03:05,070 --> 00:03:08,040 Because remember, in order for us to take an action, 62 00:03:08,040 --> 00:03:09,750 we have to let the interpreter know, 63 00:03:09,750 --> 00:03:13,743 hey, I want to run say_hello. 64 00:03:14,910 --> 00:03:19,650 Now the reason functions are so powerful 65 00:03:19,650 --> 00:03:23,640 is because of the principle that we've talked about, right? 66 00:03:23,640 --> 00:03:28,500 The idea of DRY, which stands for Do not Repeat Yourself. 67 00:03:28,500 --> 00:03:30,630 Functions are really, really useful 68 00:03:30,630 --> 00:03:34,440 when you have things that you wanna do over and over. 69 00:03:34,440 --> 00:03:37,560 For example, the print function, we've used it a lot. 70 00:03:37,560 --> 00:03:41,550 Imagine if we had to code that ourselves every single time 71 00:03:41,550 --> 00:03:44,850 and say what print function does. 72 00:03:44,850 --> 00:03:46,710 Luckily, Python gives us print 73 00:03:46,710 --> 00:03:49,410 because it's such a useful tool, 74 00:03:49,410 --> 00:03:53,550 but if in our program, we wanna say hello multiple times, 75 00:03:53,550 --> 00:03:56,460 and you can imagine this actually being a lot 76 00:03:56,460 --> 00:03:59,100 more complicated, maybe 10 lines of code. 77 00:03:59,100 --> 00:04:02,550 Instead of writing those 10 lines of code over and over, 78 00:04:02,550 --> 00:04:05,010 I can just define it as a function 79 00:04:05,010 --> 00:04:08,040 and use it anywhere I want in my program. 80 00:04:08,040 --> 00:04:11,100 For example, remember this, 81 00:04:11,100 --> 00:04:16,100 where we had a picture and we printed a Christmas tree? 82 00:04:18,510 --> 00:04:23,430 Imagine if we wanted to run this multiple times. 83 00:04:23,430 --> 00:04:24,750 Well, in order to do that, 84 00:04:24,750 --> 00:04:29,750 I would copy and paste this code and then add it again. 85 00:04:30,330 --> 00:04:34,620 And if I click run, I now have two Christmas trees. 86 00:04:34,620 --> 00:04:36,690 But that's, look at that 26 lines of code. 87 00:04:36,690 --> 00:04:39,630 I just copy and pasted the same thing over and over. 88 00:04:39,630 --> 00:04:42,963 With a function, we can do something like this. 89 00:04:44,670 --> 00:04:46,817 I can say, def show_tree(): 90 00:04:52,140 --> 00:04:53,970 and we now have a function. 91 00:04:53,970 --> 00:04:56,700 But remember the indentation in Python, right? 92 00:04:56,700 --> 00:04:57,930 Indentation is important. 93 00:04:57,930 --> 00:04:59,010 We have the semi-colon. 94 00:04:59,010 --> 00:05:00,030 We're defining a function, 95 00:05:00,030 --> 00:05:03,240 so we have to create that code block inside saying, 96 00:05:03,240 --> 00:05:07,230 hey whatever's indented here, that's part of this function. 97 00:05:07,230 --> 00:05:10,383 So now that we have the show_tree, check this out. 98 00:05:11,220 --> 00:05:15,180 I can say show_tree, run it again. 99 00:05:15,180 --> 00:05:17,823 Maybe let's run it three times. 100 00:05:20,610 --> 00:05:25,610 If I click run, how cool is that? 101 00:05:25,860 --> 00:05:28,359 I'm able to do the same thing over and over 102 00:05:28,359 --> 00:05:31,530 by just calling this function. 103 00:05:31,530 --> 00:05:34,650 And that's the power of functions. 104 00:05:34,650 --> 00:05:37,920 Functions allow us to keep our code DRY. 105 00:05:37,920 --> 00:05:41,340 We don't repeat ourselves and reuse things 106 00:05:41,340 --> 00:05:44,313 that our machines can do over and over. 107 00:05:45,330 --> 00:05:49,110 And the beauty is that this stays in memory for us. 108 00:05:49,110 --> 00:05:51,960 Show_tree now means something to this program 109 00:05:51,960 --> 00:05:53,400 because we've created it. 110 00:05:53,400 --> 00:05:58,400 So we have our own custom action that we can take. 111 00:05:58,500 --> 00:06:03,500 Now, what happens if I move show_tree here to the top? 112 00:06:06,720 --> 00:06:11,370 Well, if I run this, I'll get an error, name error. 113 00:06:11,370 --> 00:06:14,430 Name show_tree is not defined. 114 00:06:14,430 --> 00:06:16,020 Why is that? 115 00:06:16,020 --> 00:06:19,410 Well, because our interpreter goes line by line. 116 00:06:19,410 --> 00:06:23,880 It first says, all right, picture equals to this value. 117 00:06:23,880 --> 00:06:28,170 And then it goes to line 11 and says, run show_tree. 118 00:06:28,170 --> 00:06:30,750 But we haven't defined show_tree yet. 119 00:06:30,750 --> 00:06:33,990 So the Python interpreter is going to error out and say, 120 00:06:33,990 --> 00:06:36,390 hey, I have no idea what show_tree is, 121 00:06:36,390 --> 00:06:37,950 what are you talking about? 122 00:06:37,950 --> 00:06:40,860 Instead, with a function, 123 00:06:40,860 --> 00:06:43,590 we need to make sure that we define the functions 124 00:06:43,590 --> 00:06:44,640 at the beginning. 125 00:06:44,640 --> 00:06:47,043 So that Python interpreter says, 126 00:06:47,043 --> 00:06:50,610 all right, show_tree now means this. 127 00:06:50,610 --> 00:06:51,630 I'm not going to run it. 128 00:06:51,630 --> 00:06:52,770 I'm not going to use it yet. 129 00:06:52,770 --> 00:06:54,900 I'm just gonna keep it in memory. 130 00:06:54,900 --> 00:06:58,380 And when I finally come across show_tree, 131 00:06:58,380 --> 00:06:59,850 I'll know that it means something. 132 00:06:59,850 --> 00:07:03,390 I'm going to grab it from memory using show_tree 133 00:07:03,390 --> 00:07:06,780 and then I'm going to run it using the brackets. 134 00:07:06,780 --> 00:07:11,780 For example, if I do print(show_tree) without anything 135 00:07:12,300 --> 00:07:13,650 without the brackets, 136 00:07:13,650 --> 00:07:15,780 if I click run, 137 00:07:15,780 --> 00:07:20,250 you see that I get function show_tree at this location. 138 00:07:20,250 --> 00:07:22,410 This is just the location in memory. 139 00:07:22,410 --> 00:07:24,990 This is the bookshelf where we store 140 00:07:24,990 --> 00:07:27,630 that show_tree function. 141 00:07:27,630 --> 00:07:30,090 Very, very cool. 142 00:07:30,090 --> 00:07:33,000 And functions are an important powerful concept 143 00:07:33,000 --> 00:07:34,080 in programming. 144 00:07:34,080 --> 00:07:35,220 And in the next video, 145 00:07:35,220 --> 00:07:38,387 we're gonna extend this and explore this a little bit more. 146 00:07:38,387 --> 00:07:40,085 I'll see you in the next one. 147 00:07:40,085 --> 00:07:40,918 Bye, bye.