1 00:00:00,570 --> 00:00:06,510 ‫Now that you're correct in creating methods that don't have a return value, we will have a look at 2 00:00:06,510 --> 00:00:10,410 ‫return values and there will be an integer, for example. 3 00:00:10,410 --> 00:00:12,300 ‫So let's go ahead and create a method. 4 00:00:12,300 --> 00:00:20,910 ‫Public static void and void is the incorrect term here because we don't want to use empty return values, 5 00:00:20,910 --> 00:00:25,230 ‫we want to use a return value of type integer, for example. 6 00:00:25,230 --> 00:00:30,150 ‫And I'm going to call that method edX and I'm going to hand over to parameters. 7 00:00:30,150 --> 00:00:34,500 ‫So para meter one and parameter two. 8 00:00:34,530 --> 00:00:39,690 ‫Now that by itself won't work because those parameters need to have a type as well. 9 00:00:39,690 --> 00:00:42,570 ‫So they need to know of which data type they are. 10 00:00:42,570 --> 00:00:51,270 ‫And in this case, I'm going to use two integers and I want to return the result of parameter one plus 11 00:00:51,270 --> 00:00:57,150 ‫parameter two, and I'm going to call them num one because it's just going to be a number and number 12 00:00:57,150 --> 00:00:57,720 ‫two. 13 00:00:57,720 --> 00:01:02,760 ‫So just for easier reading and easier understanding of what's going on. 14 00:01:02,760 --> 00:01:06,990 ‫So they are parameters, yes, but are numbers. 15 00:01:06,990 --> 00:01:12,450 ‫And that makes it easier to understand that this method is going to add number one and number two. 16 00:01:12,450 --> 00:01:12,990 ‫All right. 17 00:01:12,990 --> 00:01:15,420 ‫So it's going to return an integer. 18 00:01:15,420 --> 00:01:17,490 ‫And now we still get an error here. 19 00:01:17,490 --> 00:01:21,330 ‫And it says program, dot add program is our class. 20 00:01:21,420 --> 00:01:22,440 ‫That is the method. 21 00:01:22,470 --> 00:01:25,530 ‫Not all code paths return a value. 22 00:01:25,530 --> 00:01:32,790 ‫What that means is that we need to have a return statement in every single path that we have. 23 00:01:32,790 --> 00:01:37,170 ‫And so far we only have one path and we will get to know other paths later on. 24 00:01:37,170 --> 00:01:43,440 ‫So if we, for example, have, if else statements where if one condition is true, then we want to 25 00:01:43,440 --> 00:01:48,900 ‫return X, and if another condition is true, then we want to return Y, for example. 26 00:01:48,900 --> 00:01:54,570 ‫So in this case we only want to return one thing, and that is going to be number one plus num two. 27 00:01:55,230 --> 00:01:59,640 ‫So this method should simply add the two values. 28 00:01:59,640 --> 00:02:11,940 ‫And if I call that method add and in here, let's say I add an argument of 15 and 31, then this program 29 00:02:11,940 --> 00:02:16,230 ‫here will simply add those two numbers together and give us the sum. 30 00:02:16,230 --> 00:02:22,860 ‫So if you run that, nothing happens or the command prompt opens real quick or a console and then it 31 00:02:22,860 --> 00:02:25,290 ‫exits because there's nothing else to do. 32 00:02:25,290 --> 00:02:28,380 ‫It did calculation and then, well, we didn't hold it. 33 00:02:28,380 --> 00:02:30,840 ‫We didn't say, come on, we want to see something. 34 00:02:30,840 --> 00:02:33,660 ‫So if we want to see something, there are two ways. 35 00:02:33,660 --> 00:02:42,330 ‫Either I create a variable which is going to be the result and I'm going to store the result of our 36 00:02:42,330 --> 00:02:45,900 ‫add return inside of this variable. 37 00:02:46,140 --> 00:02:50,610 ‫Now, the next step would be to simply print that onto our console. 38 00:02:50,610 --> 00:02:57,720 ‫So I'm going to write a line result and I'm going to read the whole thing. 39 00:02:58,500 --> 00:03:04,470 ‫So if I run that, I'll see the result of 46 and that's great. 40 00:03:04,470 --> 00:03:08,070 ‫But there is another way I can make it even simpler. 41 00:03:08,070 --> 00:03:10,200 ‫So I don't need this whole line here. 42 00:03:10,740 --> 00:03:20,610 ‫I could simply say here, add 15 plus 31 and I can get rid of this whole line of code because what that 43 00:03:20,610 --> 00:03:28,950 ‫will do is it will simply write the result of or the return value of the call of this method. 44 00:03:28,950 --> 00:03:36,720 ‫So you can use a method as an argument because to be honest, we have used arguments all the time. 45 00:03:36,720 --> 00:03:42,510 ‫Whenever we use the right line method, we use arguments because whatever we have written in here, 46 00:03:42,540 --> 00:03:45,420 ‫my text, that's an argument. 47 00:03:45,420 --> 00:03:47,070 ‫It's an argument of type string. 48 00:03:47,610 --> 00:03:52,380 ‫And now if I say ADD, now this is an argument as well. 49 00:03:52,410 --> 00:03:57,930 ‫It's an argument which is the return of my ADD method, which is of type integer. 50 00:03:57,930 --> 00:04:01,590 ‫So this one here simply represents 46. 51 00:04:02,100 --> 00:04:11,370 ‫And what I can do as well, I can call my ADD method and I can call my add method within my ADD method 52 00:04:11,370 --> 00:04:23,550 ‫so I could add one plus two and then add three and four, and that would be still totally fine. 53 00:04:23,550 --> 00:04:26,550 ‫So this looks a little confusing, but no worries. 54 00:04:26,550 --> 00:04:28,830 ‫That's usually not something that you would do. 55 00:04:29,010 --> 00:04:37,260 ‫I just wanted to show you that a method that returns something is equal with, when you call it, is 56 00:04:37,260 --> 00:04:41,070 ‫equal to the return value itself. 57 00:04:41,070 --> 00:04:46,530 ‫So in this case it would be three plus and then that would be seven. 58 00:04:46,530 --> 00:04:48,420 ‫So that result would be ten. 59 00:04:48,420 --> 00:04:53,550 ‫So if I add that console, that right line and I run the code, that's what I get. 60 00:04:53,550 --> 00:04:55,650 ‫So I get ten and 46. 61 00:04:57,920 --> 00:04:59,020 ‫All righty. 62 00:04:59,020 --> 00:05:01,210 ‫So I hope that didn't confuse you. 63 00:05:01,540 --> 00:05:10,630 ‫But what is important is that you can have return types and you can have multiple different parameters. 64 00:05:10,630 --> 00:05:11,230 ‫All right. 65 00:05:11,230 --> 00:05:19,720 ‫Now, please go ahead and create a method that multiplies two values to each other and returns the result 66 00:05:19,720 --> 00:05:21,340 ‫of that multiplication. 67 00:05:23,970 --> 00:05:24,330 ‫All right. 68 00:05:24,330 --> 00:05:25,560 ‫I hope you tried it. 69 00:05:25,770 --> 00:05:31,500 ‫So now let's go ahead and create another method, and it should return an integer as well. 70 00:05:31,830 --> 00:05:39,690 ‫Multiply and that will be int num one and int num two. 71 00:05:40,530 --> 00:05:43,170 ‫And it has to return something. 72 00:05:44,250 --> 00:05:49,830 ‫And in this case, it will return num one multiplied with num two. 73 00:05:50,830 --> 00:05:52,870 ‫And this is designed for multiplication. 74 00:05:52,870 --> 00:05:56,350 ‫So you simply use the star to multiply. 75 00:05:57,610 --> 00:06:01,020 ‫All right, now let's call that method instead. 76 00:06:01,030 --> 00:06:06,280 ‫So multiply, let's say 25 times 25. 77 00:06:06,520 --> 00:06:10,450 ‫So that should be 625. 78 00:06:10,660 --> 00:06:12,970 ‫And there we are, 625. 79 00:06:13,120 --> 00:06:19,410 ‫So you see, you can use that for adding for multiplying, for division as well. 80 00:06:19,420 --> 00:06:26,710 ‫So let's create a divide method static and now I'm going to use double because it can be a floating 81 00:06:26,710 --> 00:06:29,620 ‫point result and I don't want to cut it off. 82 00:06:29,620 --> 00:06:33,100 ‫I want to be as precise as possible when I do division. 83 00:06:33,100 --> 00:06:39,760 ‫So I'm going to use double here and I'm going to call that divide and it's going to divide two values 84 00:06:39,760 --> 00:06:49,960 ‫from each other or one through the other, and return none one divided by num two. 85 00:06:52,400 --> 00:06:54,200 ‫And this one needs a type as well. 86 00:06:54,200 --> 00:06:57,710 ‫So in this case, I'm using two integers. 87 00:06:58,220 --> 00:07:00,890 ‫Let's see what happens, because I return a double. 88 00:07:00,890 --> 00:07:03,800 ‫But as parameters, I still use strings. 89 00:07:04,460 --> 00:07:09,680 ‫So let's divide 25 by 13. 90 00:07:11,900 --> 00:07:12,950 ‫And we get one. 91 00:07:12,950 --> 00:07:13,970 ‫Why do we get one? 92 00:07:13,970 --> 00:07:15,020 ‫That's so weird. 93 00:07:15,440 --> 00:07:18,640 ‫5025 divided by 13 is not one. 94 00:07:18,650 --> 00:07:20,840 ‫It's actually almost two. 95 00:07:21,140 --> 00:07:24,890 ‫But the problem lies in those integers here. 96 00:07:24,890 --> 00:07:30,320 ‫So we are dividing an integer through an integer and the result will be an integer. 97 00:07:30,890 --> 00:07:34,280 ‫And then what we do is return it as a double. 98 00:07:34,940 --> 00:07:41,990 ‫So this one here is 25 divided by 13, which is 1.9, and then you just cut off the nine. 99 00:07:41,990 --> 00:07:46,010 ‫So it's 1.0, and that is what is double returns. 100 00:07:46,010 --> 00:07:48,230 ‫So it simply returns one. 101 00:07:48,230 --> 00:07:57,740 ‫So if we instead use doubles here, so we divide divide a double through a double, then we get this 102 00:07:57,740 --> 00:08:01,280 ‫1.9 or 1.9, two, three and so forth. 103 00:08:01,370 --> 00:08:04,250 ‫So you have to be very careful with your types. 104 00:08:04,250 --> 00:08:12,620 ‫So whenever you use division, check out what the result is that you want to you want to cut off anything 105 00:08:12,620 --> 00:08:19,910 ‫behind the floating point and using integers totally fine but otherwise use bubble great. 106 00:08:19,910 --> 00:08:21,830 ‫So this will just some examples. 107 00:08:21,830 --> 00:08:27,980 ‫You can of course not only use integers here, you can use strings, you can return strings, you can 108 00:08:27,980 --> 00:08:35,690 ‫return not only our basic data types, you can also return whole objects, complete objects. 109 00:08:35,690 --> 00:08:41,510 ‫And that is something that we will cover much later when we handle objects. 110 00:08:41,600 --> 00:08:44,600 ‫I'd say that's a good start for methods. 111 00:08:44,600 --> 00:08:50,960 ‫Please go ahead and try to create some methods of your own and that will be a challenge that will give 112 00:08:50,960 --> 00:08:52,310 ‫you some instructions. 113 00:08:52,310 --> 00:08:52,760 ‫Great. 114 00:08:52,760 --> 00:08:54,170 ‫So see you in the next video.