1 00:00:00,000 --> 00:00:04,380 Now is the time to start working with functions. A 2 00:00:04,380 --> 00:00:07,800 function is a reusable block of code that you can 3 00:00:07,800 --> 00:00:11,310 reuse later in your program as many times as you 4 00:00:11,310 --> 00:00:15,030 want. So why do you need functions? Well, this 5 00:00:15,030 --> 00:00:17,550 will help you create building blocks in your 6 00:00:17,550 --> 00:00:21,060 program that you can use and reuse on top of each 7 00:00:21,060 --> 00:00:23,760 other, so you can increase the complexity of the 8 00:00:23,760 --> 00:00:26,850 application, without increasing the complexity of 9 00:00:26,850 --> 00:00:30,570 your code. Your code will stay clean, and easy to 10 00:00:30,570 --> 00:00:34,020 modify. And instead of giving you many theoretical 11 00:00:34,020 --> 00:00:37,860 explanations, let's dive in and write our first 12 00:00:37,860 --> 00:00:41,340 function. Okay, I'm going to remove all of that, 13 00:00:41,760 --> 00:00:44,400 I'm going to create a function. So to write a 14 00:00:44,400 --> 00:00:47,670 function, you first need to write the def, def and 15 00:00:47,670 --> 00:00:50,310 then space, and then the name of the function. 16 00:00:50,340 --> 00:00:53,370 Let's say we want to function that says hello. So 17 00:00:53,370 --> 00:00:58,200 I'm going to write say_hello. That is the name of 18 00:00:58,200 --> 00:01:00,690 the function. After the name of the function, you 19 00:01:00,690 --> 00:01:04,650 need to open and close parentheses, and then add a 20 00:01:04,680 --> 00:01:08,010 colon. After this, you can press enter, and as 21 00:01:08,010 --> 00:01:11,550 you can see, the marker here doesn't, the cursor 22 00:01:11,580 --> 00:01:16,230 doesn't come back to here. It come back to here 23 00:01:16,290 --> 00:01:21,270 with, as you can see here, 1234 new spaces, which 24 00:01:21,270 --> 00:01:24,900 is called the indentation, okay. So you're going 25 00:01:24,900 --> 00:01:28,260 to write everything under this function with an 26 00:01:28,290 --> 00:01:31,680 indentation of four spaces. So I can write any 27 00:01:31,680 --> 00:01:35,640 instruction I want. So for example, print("Hello"). 28 00:01:36,120 --> 00:01:40,020 Okay, that's a very basic function that just says 29 00:01:40,050 --> 00:01:44,340 Hello. So def,name of the function. So again, 30 00:01:44,340 --> 00:01:46,980 what you can do, if you want to add multiple 31 00:01:46,980 --> 00:01:51,284 words, you can add an underscore, and then parenthesis colon, 32 00:01:51,284 --> 00:01:54,000 and everything you write inside the 33 00:01:54,000 --> 00:01:57,270 function will be with an indentation. Now, if I 34 00:01:57,270 --> 00:02:01,020 run that code, let's run the main, you can see, 35 00:02:01,020 --> 00:02:04,230 well, we have nothing. Why is that? Because we 36 00:02:04,230 --> 00:02:08,789 have just created a function, okay, we didn't call 37 00:02:08,820 --> 00:02:11,640 the function, okay. If you just create, if you 38 00:02:11,640 --> 00:02:14,130 just define a function like this, it is not going to 39 00:02:14,130 --> 00:02:17,280 do anything. What you need to do in your code is 40 00:02:17,280 --> 00:02:19,440 to call the function, and to call the function, you 41 00:02:19,440 --> 00:02:23,400 just write the name of the function, okay, with 42 00:02:23,400 --> 00:02:27,480 the parentheses. And then you can run the 43 00:02:27,480 --> 00:02:30,600 again, and you can see Hello. So the good thing 44 00:02:30,600 --> 00:02:33,480 about a function is that you can actually call it 45 00:02:33,480 --> 00:02:37,680 multiple times. So I can just copy this and paste 46 00:02:37,680 --> 00:02:39,810 it three times. And it's going to call the 47 00:02:39,810 --> 00:02:43,170 function three times. You can see Hello, Hello, 48 00:02:43,200 --> 00:02:46,380 Hello. So of course, make sure you define the 49 00:02:46,470 --> 00:02:49,170 function before you call it okay, otherwise, 50 00:02:49,170 --> 00:02:51,930 you're going to get an error. And then as you can 51 00:02:51,930 --> 00:02:55,110 see here, everything that is indented will be 52 00:02:55,290 --> 00:02:58,530 under the function. So let's say I write print, 53 00:02:58,530 --> 00:03:03,090 let's say I write test, okay. I can run again and you can 54 00:03:03,090 --> 00:03:06,990 see Hello, test, Hello, test, Hello, test. So if I 55 00:03:06,990 --> 00:03:10,620 continue to add lines after this one, but with an 56 00:03:10,650 --> 00:03:14,580 indentation of four spaces, it's going to be under 57 00:03:14,580 --> 00:03:18,330 the function. When I go back to the default 58 00:03:18,360 --> 00:03:21,510 indentation of zero spaces, we are back to the 59 00:03:21,510 --> 00:03:24,180 main program and then so we can call the function 60 00:03:24,200 --> 00:03:25,900 directly like this.