1 00:00:00,000 --> 00:00:04,000 Now is the time to start working with functions. 2 00:00:04,001 --> 00:00:10,000 A function is a reusable block of code that you can 3 00:00:10,001 --> 00:00:12,000 reuse later in your program as many times as you want. 4 00:00:12,001 --> 00:00:14,000 So why do you need functions? 5 00:00:14,001 --> 00:00:18,000 Well, this will help you create building blocks in your program 6 00:00:18,001 --> 00:00:21,000 that you can use and reuse on top of each other 7 00:00:21,001 --> 00:00:25,000 so you can increase the complexity of the application 8 00:00:25,001 --> 00:00:28,000 without increasing the complexity of your code. 9 00:00:28,001 --> 00:00:31,000 Your code will stay clean and easy to modify. 10 00:00:31,001 --> 00:00:35,000 And instead of giving you many theoretical explanations, 11 00:00:35,001 --> 00:00:39,000 let's dive in and write our first function. 12 00:00:39,001 --> 00:00:41,000 Okay, I'm going to remove all of that. 13 00:00:41,001 --> 00:00:44,000 And I'm going to create a function. 14 00:00:44,001 --> 00:00:47,000 So to create a function, you first need to write Dev. 15 00:00:47,001 --> 00:00:50,000 Dev and then space and then the name of the function. 16 00:00:50,001 --> 00:00:53,000 Let's say we want a function that says hello. 17 00:00:53,001 --> 00:00:56,000 So I'm going to write, say hello. 18 00:00:57,000 --> 00:00:59,000 That is the name of the function. 19 00:00:59,001 --> 00:01:03,000 After the name of the function, you need to open 20 00:01:03,001 --> 00:01:05,000 and close parentheses and then add a column. 21 00:01:05,001 --> 00:01:07,000 After this, you can press enter. 22 00:01:07,001 --> 00:01:14,000 And as you can see, the marker here doesn't, the cursor doesn't come back to here. 23 00:01:14,001 --> 00:01:18,000 It comebacks to here with, as you can see here, 24 00:01:18,001 --> 00:01:23,000 one, two, three, four new spaces, which is called the indentation. 25 00:01:24,000 --> 00:01:28,000 So you're going to write everything under this 26 00:01:28,001 --> 00:01:30,000 function with an indentation of four spaces. 27 00:01:30,001 --> 00:01:33,000 So I can write any instruction I want. 28 00:01:33,001 --> 00:01:36,000 So for example, print hello. 29 00:01:36,001 --> 00:01:40,000 Okay, that's a very basic function that just says hello. 30 00:01:40,001 --> 00:01:43,000 So Dev, name of the function. 31 00:01:43,001 --> 00:01:47,000 So again, what you can do if you want to add 32 00:01:47,001 --> 00:01:49,000 multiple words, you can add an underscore. 33 00:01:49,001 --> 00:01:51,000 And then parentheses, column, 34 00:01:52,000 --> 00:01:57,000 and everything you write inside the function will be with an indentation. 35 00:01:57,001 --> 00:02:02,000 Now, if I run that code, let's run the main, you can see, well, we have nothing. 36 00:02:02,001 --> 00:02:03,000 Why is that? 37 00:02:03,001 --> 00:02:07,000 Because we have just created a function. 38 00:02:07,001 --> 00:02:10,000 Okay, we didn't call the function. 39 00:02:10,001 --> 00:02:13,000 Okay, if you just create, if you just define a 40 00:02:13,001 --> 00:02:15,000 function like this, it's not going to do anything. 41 00:02:15,001 --> 00:02:18,000 What you need to do in your code is to call the function. 42 00:02:18,001 --> 00:02:21,000 To call the function, you just write the name of the function. 43 00:02:22,000 --> 00:02:24,000 Okay, with the parentheses. 44 00:02:24,001 --> 00:02:29,000 And then you can run the program again, and you can see hello. 45 00:02:29,001 --> 00:02:34,000 So the good thing about a function is that you can actually call it multiple times. 46 00:02:34,001 --> 00:02:39,000 So I can just copy this and paste it three times. 47 00:02:39,001 --> 00:02:41,000 And it's going to call the function three times. 48 00:02:41,001 --> 00:02:44,000 You can see hello, hello, hello. 49 00:02:44,001 --> 00:02:48,000 So of course, make sure you define the function before you call it. 50 00:02:48,001 --> 00:02:51,000 Okay, otherwise, you're going to get an error. 51 00:02:51,001 --> 00:02:54,000 And then, well, as you can see here, everything 52 00:02:54,001 --> 00:02:56,000 that is indented will be under the function. 53 00:02:56,001 --> 00:03:00,000 So let's say I write print, let's say I write test. 54 00:03:00,001 --> 00:03:06,000 Okay, I can run again, and you can see hello, test, hello, test, hello, test. 55 00:03:06,001 --> 00:03:10,000 So if I continue to add lines after this one, 56 00:03:10,001 --> 00:03:15,000 but with an indentation of four spaces, it's going to be under the function. 57 00:03:15,001 --> 00:03:21,000 When I go back to the default indentation of zero spaces, 58 00:03:21,001 --> 00:03:23,489 we are back to the main program, and then so 59 00:03:23,501 --> 00:03:26,000 we can call the function directly like this.