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