1 00:00:03,680 --> 00:00:09,590 We have situations, one, we have to do things repeatedly for that we have loops. 2 00:00:09,860 --> 00:00:10,310 All right. 3 00:00:10,310 --> 00:00:16,820 But we would like to run code based on different conditions, but we can use controls for that. 4 00:00:17,030 --> 00:00:24,580 And we use methods if we would like to wrap these into a block and reuse it for different purposes. 5 00:00:25,930 --> 00:00:33,600 To explain what methods are, I'm going to use an example that my math teacher gave me in primary school, 6 00:00:34,270 --> 00:00:36,330 maybe you have seen something similar. 7 00:00:36,550 --> 00:00:41,020 So we had imaginary machine in primary school. 8 00:00:41,350 --> 00:00:44,740 The machine had one input and one output. 9 00:00:45,070 --> 00:00:47,790 And we had to figure out what the machine does. 10 00:00:48,250 --> 00:00:53,230 For example, an input like five and we had an output 15. 11 00:00:53,480 --> 00:01:00,730 OK, so what the machine does, it wasn't obvious the time, but the machine multiplies the number that 12 00:01:00,730 --> 00:01:03,910 goes and by free and the result comes out. 13 00:01:05,090 --> 00:01:10,430 We also had exercises when the machine had two inputs and one output. 14 00:01:11,450 --> 00:01:14,660 Five and Fragos and 15 comes out. 15 00:01:15,780 --> 00:01:19,350 And the machine just simply multiplies the two numbers. 16 00:01:20,300 --> 00:01:27,290 The good thing about these machines is that they can be reused with other numbers and they can just 17 00:01:27,440 --> 00:01:28,340 do their job. 18 00:01:29,300 --> 00:01:38,150 In Java, you can have machines or methods that have a lot of inputs or machines that don't have an 19 00:01:38,150 --> 00:01:46,490 input or output, it just prints out a text two times or it has an input, but there is no output more 20 00:01:46,490 --> 00:01:48,080 than printing out something. 21 00:01:49,690 --> 00:01:57,580 By definition, a method or a function is a block of code, which only runs when it is called. 22 00:01:58,830 --> 00:02:03,750 Functions are one of the most important features of all programming languages. 23 00:02:04,230 --> 00:02:08,390 It is very important to understand them and use them the correct way. 24 00:02:09,280 --> 00:02:15,990 The takeaway from here is that a method is a block of code that runs only when it is called. 25 00:02:16,560 --> 00:02:20,100 They have inputs and outputs, if there is any. 26 00:02:20,730 --> 00:02:24,210 And their biggest advantage is that they are reusable. 27 00:02:25,400 --> 00:02:32,450 So what we are going to cover in this lecture, first, we are going to look into how to create a method, 28 00:02:32,630 --> 00:02:35,180 then how to call a method. 29 00:02:35,420 --> 00:02:39,710 After that, I will tell you more about parameters and arguments. 30 00:02:40,130 --> 00:02:42,320 What are the differences then? 31 00:02:42,320 --> 00:02:43,560 Return values. 32 00:02:43,580 --> 00:02:50,400 And finally, I will show you what the function overloading is and how to do that after this lecture. 33 00:02:50,420 --> 00:02:52,400 That will be according to. 34 00:02:53,690 --> 00:02:55,240 All right, let's do this. 35 00:02:58,100 --> 00:03:06,350 OK, so how we can define a method, first and foremost, it must be declared within a class, the most 36 00:03:06,350 --> 00:03:13,640 basic way to define a method is that to start with the day to type, that the function returns, then 37 00:03:14,030 --> 00:03:17,420 the function name next parentheses. 38 00:03:17,840 --> 00:03:21,650 After that, curly braces with a return statement. 39 00:03:22,610 --> 00:03:26,920 The returned values data type must match with the return type. 40 00:03:27,530 --> 00:03:32,170 The return type can be void, which means that the function doesn't return anything. 41 00:03:32,570 --> 00:03:37,610 In that case, we don't need the return statement, just like in the following example. 42 00:03:37,820 --> 00:03:41,240 This say good morning function simply prints out. 43 00:03:41,240 --> 00:03:42,050 Good morning. 44 00:03:43,300 --> 00:03:50,980 Function definition can be more complex than this, you can add more parameters to definition as modifiers 45 00:03:50,980 --> 00:03:53,370 can be public, private or package. 46 00:03:53,650 --> 00:03:56,260 We will cover these later in discourse. 47 00:03:56,950 --> 00:04:02,050 Static shows Javor that the function belongs to the class instead of an object. 48 00:04:02,680 --> 00:04:09,700 This is also a topic that I will tell you more after you will have knowledge and classes and objects. 49 00:04:10,180 --> 00:04:13,810 The return type and the function name have been mentioned previously. 50 00:04:14,320 --> 00:04:21,910 Parameters are optional, parameters are passed and can be used in the functions block. 51 00:04:22,600 --> 00:04:25,030 You have already used such functions. 52 00:04:25,240 --> 00:04:26,830 For example, the main method. 53 00:04:27,940 --> 00:04:34,320 The public is an access modifier, static shows that it belongs to the class well. 54 00:04:35,030 --> 00:04:43,330 It shows that it doesn't return a value made is the function name, then a parentheses string square 55 00:04:43,330 --> 00:04:49,150 brackets shows that it is an array of strings and it is named ARGs. 56 00:04:50,640 --> 00:04:59,940 Now, let's see a function that's a bit less complex, get 10 percent of discount price, takes an enterprise 57 00:05:00,480 --> 00:05:10,590 and multiply it by zero point nine and returns the result because we multiply a number by zero point 58 00:05:10,590 --> 00:05:13,850 nine, we will get a nonpoor number. 59 00:05:14,460 --> 00:05:17,520 So the return data type is double. 60 00:05:18,540 --> 00:05:25,860 One additional thing I would recommend you is that the function should do one thing and its name must 61 00:05:25,860 --> 00:05:28,350 tell what the function really does. 62 00:05:30,530 --> 00:05:39,040 To call a method in Java, right, the methods name first, followed by two parentheses with the parameters, 63 00:05:39,050 --> 00:05:48,260 if there is any, and a semicolon in the case of get ten percent of discount price, we can use directly 64 00:05:48,650 --> 00:05:54,050 twenty thousand as an input or we could use a variable name like Price. 65 00:05:54,990 --> 00:06:03,330 There are other ways to use it, like a sign the output or the return value to a variable, as you can 66 00:06:03,330 --> 00:06:08,770 see, the variables data type matches the functions return data type. 67 00:06:09,240 --> 00:06:16,390 You can also use the function as a parameter for any other functions, like, in our case, for Quintel 68 00:06:16,410 --> 00:06:17,160 and function. 69 00:06:18,060 --> 00:06:20,790 Now let's move on to parameters and arguments. 70 00:06:22,020 --> 00:06:29,070 Information can be passed to methods, true parameters, they are specified after the functions name 71 00:06:29,520 --> 00:06:38,490 inside the parentheses and separated with commas, parameters act as variables inside the methods. 72 00:06:39,430 --> 00:06:45,850 A method can have an unlimited amount of parameters, but I suggest you not to have more than three, 73 00:06:46,690 --> 00:06:55,540 the following example has a method that takes to end as parameters and returns the discount percentage 74 00:06:55,540 --> 00:06:56,380 of the price. 75 00:06:57,420 --> 00:07:00,820 Now, let's see what's the difference between parameters and arguments. 76 00:07:01,170 --> 00:07:08,820 We have a defined function here and a case where we call the function when a parameter is passed to 77 00:07:08,820 --> 00:07:11,200 the method and is called an argument. 78 00:07:11,670 --> 00:07:20,310 So from the example here and price is a parameter, while twenty thousand is an argument. 79 00:07:21,440 --> 00:07:28,180 Know that when you're working with multiple parameters, the method call must have the same number of 80 00:07:28,180 --> 00:07:34,860 arguments as there are parameters and the arguments must be passed in the same order. 81 00:07:36,730 --> 00:07:44,320 Moving on to return values, when you use a function to return a value, the return type must match 82 00:07:44,470 --> 00:07:47,560 with the data type of the value that return. 83 00:07:48,160 --> 00:07:52,420 That data type can be anything a primitive or an object. 84 00:07:53,750 --> 00:08:00,290 When your function doesn't return a value, the function must use void instead of a return type. 85 00:08:00,660 --> 00:08:07,640 Additionally, there is no return statement in the function like in the case of say good morning function. 86 00:08:09,250 --> 00:08:16,630 Our last topic is overloading with method, overloading, multiply methods can have the same name with 87 00:08:16,630 --> 00:08:17,990 different parameters. 88 00:08:18,580 --> 00:08:19,720 Why would we need that? 89 00:08:20,560 --> 00:08:26,230 Because we can have functions that do the same thing on different data types. 90 00:08:26,500 --> 00:08:32,330 And instead of using different names for the same purpose, we use overloading. 91 00:08:33,100 --> 00:08:37,500 OK, this go template for overloading may not make it clear. 92 00:08:38,080 --> 00:08:40,120 So move on to the examples. 93 00:08:41,490 --> 00:08:49,440 So here we have the same functions, same definition, same return statement, but the parameters are 94 00:08:49,440 --> 00:08:52,770 different with having just the end version. 95 00:08:53,220 --> 00:09:01,560 We couldn't use double or float values or we should convert them into an end by having a function with 96 00:09:01,560 --> 00:09:02,810 a double parameter. 97 00:09:03,090 --> 00:09:09,840 We can use decimal numbers with the same name based on the data type we pass to the function. 98 00:09:10,110 --> 00:09:12,930 Java will know which functions should be called. 99 00:09:13,970 --> 00:09:20,480 So that's it, I hope it was clear if it wasn't, I will try to make it more understandable in the coding 100 00:09:20,480 --> 00:09:20,900 part.