1 00:00:00,850 --> 00:00:03,440 It's time to talk about functions. 2 00:00:03,910 --> 00:00:12,670 This is where things get really interesting because up until now we've seen functions write functions 3 00:00:12,670 --> 00:00:21,550 where things like print, like list Bolian, we even saw the input function to get the input of whatever 4 00:00:21,550 --> 00:00:26,880 the user types and those allowed us to perform actions on our data types. 5 00:00:27,490 --> 00:00:32,380 But the true power comes when we can start creating our own functions. 6 00:00:32,680 --> 00:00:33,190 That's right. 7 00:00:33,190 --> 00:00:35,620 We're not limited to whatever Python gives us. 8 00:00:35,890 --> 00:00:40,390 We're able to create our own functions and use them in our programs. 9 00:00:40,840 --> 00:00:43,030 So let's learn how to do that. 10 00:00:44,160 --> 00:00:50,560 The way we create a function in Python and functions, by the way, exists in all programming languages. 11 00:00:50,610 --> 00:00:52,200 They're very, very important. 12 00:00:52,740 --> 00:01:00,000 If we do D.F., that lets the Python interpreter know that we're about to define a function. 13 00:01:00,420 --> 00:01:02,410 DSF is short for define. 14 00:01:02,700 --> 00:01:06,720 So the interpreter is going to say, all right, they're about to define a function. 15 00:01:07,050 --> 00:01:08,850 What's the function going to be? 16 00:01:09,480 --> 00:01:11,520 Well, we can create whatever we want. 17 00:01:11,910 --> 00:01:16,850 We use the same name in case, as we do with variables to define our functions. 18 00:01:17,070 --> 00:01:19,080 So let's create a function again. 19 00:01:19,440 --> 00:01:24,090 We'll call it say hello, just a variable that I created. 20 00:01:24,090 --> 00:01:30,900 But this time it's a function because we use the def keyword now in here and say hello. 21 00:01:31,050 --> 00:01:36,030 We also add the brackets to let the interpreter know that this is something that we're going to take 22 00:01:36,030 --> 00:01:39,930 action on or this is going to perform an action on a data type. 23 00:01:40,410 --> 00:01:46,740 We use that, Colin, and then within this block of code we can say print. 24 00:01:51,490 --> 00:01:54,010 Is a function if I click run. 25 00:01:55,970 --> 00:01:58,180 Hmm, nothing happened. 26 00:02:01,430 --> 00:02:08,750 Well, we've created the say hello function, we've defined it, and now it's living somewhere in memory 27 00:02:08,750 --> 00:02:09,420 on our machine. 28 00:02:09,860 --> 00:02:16,520 However, in order to use a function, remember, just like we use the print function, we have to call 29 00:02:16,520 --> 00:02:17,700 it with the brackets. 30 00:02:18,080 --> 00:02:23,420 So after we define a function, we say, say hello. 31 00:02:24,230 --> 00:02:28,880 And did you notice how my Reppel as soon as I said, say. 32 00:02:29,880 --> 00:02:36,810 Actually gives me the say hello, comen, because I've created it and you see this purple box which 33 00:02:36,810 --> 00:02:43,830 shows that it's a function, well, we now have it available for us to use, just like we had the print 34 00:02:43,830 --> 00:02:45,540 function available for us to use. 35 00:02:46,080 --> 00:02:51,660 So if I do say hello and run it with the brackets and I click run. 36 00:02:54,080 --> 00:03:00,920 I get hello, how cool is that, by the way, what happens if I run it without the brackets and I click 37 00:03:00,920 --> 00:03:01,190 run? 38 00:03:03,460 --> 00:03:09,310 Nothing happens because, remember, in order for us to take an action, we have to let the interpreter 39 00:03:09,310 --> 00:03:13,600 know, hey, I want to run, say hello. 40 00:03:14,890 --> 00:03:23,290 Now, the reason functions are so powerful is because of the principle that we've talked about, right? 41 00:03:23,620 --> 00:03:30,970 The idea of dry, which stands for do not repeat yourself functions are really, really useful when 42 00:03:30,970 --> 00:03:33,900 you have things that you want to do over and over. 43 00:03:34,450 --> 00:03:37,230 For example, the brain function, we've used it a lot. 44 00:03:37,570 --> 00:03:44,320 Imagine if we had to code that ourselves every single time and say what print function does. 45 00:03:44,800 --> 00:03:48,820 Luckily, Python gives us a print because it's such a useful tool. 46 00:03:49,390 --> 00:03:56,260 But if in our program we want to say hello multiple times and you can imagine this actually being a 47 00:03:56,260 --> 00:04:01,960 lot more complicated, maybe 10 lines of code, instead of writing those ten lines of code over and 48 00:04:01,960 --> 00:04:06,820 over, I can just define it as a function and use it anywhere I want. 49 00:04:06,820 --> 00:04:16,960 In my program, for example, remember this, where we had a picture and we printed a Christmas tree. 50 00:04:18,510 --> 00:04:26,370 Imagine if we wanted to run this multiple times, well, in order to do that, I would copy and paste 51 00:04:26,400 --> 00:04:27,030 this code. 52 00:04:27,980 --> 00:04:31,430 And then added again, and if I click run. 53 00:04:32,390 --> 00:04:37,520 I now have two Christmas trees, but let's look at that, twenty six lines of code, I just copy and 54 00:04:37,520 --> 00:04:40,640 paste it the same thing over and over with a function. 55 00:04:40,670 --> 00:04:42,800 We can do something like this. 56 00:04:44,620 --> 00:04:48,160 I can say define show. 57 00:04:49,240 --> 00:04:49,720 Tre. 58 00:04:51,010 --> 00:04:57,430 Colin, and we now have a function, but remember, the indentation in Python, right indentation is 59 00:04:57,430 --> 00:04:57,780 important. 60 00:04:57,910 --> 00:04:59,050 We have the semicolon. 61 00:04:59,050 --> 00:05:00,060 We're defining a function. 62 00:05:00,070 --> 00:05:05,950 So we have to create that code block inside saying, hey, whatever is indented here, that's part of 63 00:05:05,950 --> 00:05:06,560 this function. 64 00:05:07,210 --> 00:05:10,330 So now that we have the show tree, check this out. 65 00:05:11,170 --> 00:05:15,010 I can say show tree, run it again. 66 00:05:15,160 --> 00:05:17,680 Maybe let's run it three times. 67 00:05:20,570 --> 00:05:21,530 If I click, run. 68 00:05:24,250 --> 00:05:32,230 How cool is that I'm able to do the same thing over and over by just calling this function and that's 69 00:05:32,350 --> 00:05:33,910 the power of functions. 70 00:05:34,660 --> 00:05:37,630 Functions allow us to keep our code dry. 71 00:05:37,870 --> 00:05:44,170 We don't repeat ourselves and reuse things that our machines can do over and over. 72 00:05:45,290 --> 00:05:52,370 And the beauty is that this stays a memory for us show tree now means something to this program because 73 00:05:52,370 --> 00:05:53,390 we've created it. 74 00:05:53,420 --> 00:05:57,610 So we have our own custom action that we can take. 75 00:05:58,490 --> 00:06:03,410 Now, what happens if I move show tree here to the top? 76 00:06:06,710 --> 00:06:08,120 Well, if I run this. 77 00:06:09,420 --> 00:06:13,720 I'll get an air name, air name, show three is not defined. 78 00:06:14,430 --> 00:06:15,390 Why is that? 79 00:06:16,020 --> 00:06:22,830 Well, because our interpreter goes line by line at first, says, all right, picture equals to this 80 00:06:22,830 --> 00:06:23,250 value. 81 00:06:23,850 --> 00:06:27,900 And then it goes to line eleven and says, run, show, tree. 82 00:06:28,140 --> 00:06:30,530 But we haven't defined show tree yet. 83 00:06:30,720 --> 00:06:36,410 So the python interpreter is going to air out and say, hey, I have no idea what show tree is. 84 00:06:36,420 --> 00:06:37,350 What are you talking about? 85 00:06:37,920 --> 00:06:44,310 Instead, with a function, we need to make sure that we define the functions at the beginning. 86 00:06:44,610 --> 00:06:50,370 So that Python interpreter says, all right, show tree now means this. 87 00:06:50,550 --> 00:06:51,630 I'm not going to run it. 88 00:06:51,630 --> 00:06:52,800 I'm not going to use it yet. 89 00:06:52,800 --> 00:06:54,450 I'm just going to keep it in memory. 90 00:06:54,870 --> 00:06:59,790 And when I finally come across show tree, I'll know that it means something. 91 00:06:59,820 --> 00:07:06,390 I'm going to grab it from memory using show tree, and then I'm going to run it using the brackets. 92 00:07:06,750 --> 00:07:13,350 For example, if I do print show tree without anything, without the brackets. 93 00:07:13,650 --> 00:07:14,820 If I click run. 94 00:07:15,770 --> 00:07:22,670 You see that I get function, Shokri, at this location, this is just the location and memory, this 95 00:07:22,670 --> 00:07:29,150 is the bookshelf where we store that show tree function very, very cool. 96 00:07:30,050 --> 00:07:33,740 And functions are an important, powerful concept in programming. 97 00:07:34,010 --> 00:07:37,940 And in the next video, we're going to extend this and explore this a little bit more. 98 00:07:38,700 --> 00:07:39,600 I'll see in the next one.