1 00:00:00,150 --> 00:00:05,460 Welcome back in this video, we are going to look at functions and functions are something that we have 2 00:00:05,460 --> 00:00:06,330 used already. 3 00:00:06,360 --> 00:00:11,730 For example, the main function and now we're going to see how we can create our own functions, which 4 00:00:11,730 --> 00:00:15,360 we then can use to reuse code very easily. 5 00:00:15,690 --> 00:00:20,700 And in order to do so, I'm going to clean up this whole main function now because it's going to be 6 00:00:21,000 --> 00:00:25,140 a lot clearer if we have everything on one screen. 7 00:00:25,590 --> 00:00:27,000 So we have this main function. 8 00:00:27,000 --> 00:00:31,890 And as you know, this is the entry point of our program and that's where everything begins. 9 00:00:31,890 --> 00:00:35,850 So if no code isn't here, then nothing will happen if we start to program. 10 00:00:36,180 --> 00:00:37,620 So that's important to know. 11 00:00:37,890 --> 00:00:39,270 And you know that already. 12 00:00:39,630 --> 00:00:46,260 So the next thing we can do is we can create code outside of this function and then call it within this 13 00:00:46,260 --> 00:00:46,680 function. 14 00:00:46,980 --> 00:00:52,230 And in order to do so, you can just go ahead and create a new function with the keyboard fun. 15 00:00:52,800 --> 00:00:57,390 And then you give this a name, and I'm just going to call this one my function. 16 00:00:59,290 --> 00:01:02,710 And this is generally the structure that you have, so you have this keyword fund. 17 00:01:03,040 --> 00:01:05,170 Then you have a name for the function. 18 00:01:05,620 --> 00:01:08,560 You have round that brackets and then you have curly brackets. 19 00:01:08,740 --> 00:01:14,860 And within the rounded brackets, you can put all your parameters within the curly brackets. 20 00:01:14,890 --> 00:01:19,420 You can add to code block that should be executed once this function is called. 21 00:01:20,020 --> 00:01:23,410 So what do all of these terms mean and what we will see in the second? 22 00:01:23,410 --> 00:01:26,770 So let's get started first with something very basic. 23 00:01:26,770 --> 00:01:34,030 So I'm just going to use print here, and I'm just going to say called from my function. 24 00:01:35,260 --> 00:01:35,730 All right. 25 00:01:35,740 --> 00:01:37,980 So now let's just call this my function. 26 00:01:37,990 --> 00:01:39,340 How do we call a function? 27 00:01:39,730 --> 00:01:46,510 Well, we call it by just using the name of the function and then the brackets here. 28 00:01:47,200 --> 00:01:51,850 And if we have any arguments that we want to pass, then we need to pass them as well. 29 00:01:52,480 --> 00:01:55,600 So let's run this code just to see if this is working. 30 00:01:56,780 --> 00:02:00,770 And once you run it, you will see that it's going to say call from my function. 31 00:02:01,370 --> 00:02:01,730 All right. 32 00:02:01,730 --> 00:02:07,400 So this print statement that was within my function, so within the function that we called my function 33 00:02:07,760 --> 00:02:10,850 was called once we called this function here. 34 00:02:11,090 --> 00:02:17,990 So if we decide to call this function twice, then it's going to display called for my function twice 35 00:02:17,990 --> 00:02:22,550 here and that we are called for my function and again called for my function. 36 00:02:22,850 --> 00:02:27,040 And as we are using a print statement, they are told directly after another. 37 00:02:27,050 --> 00:02:31,610 So there is no space in between or a no line break either. 38 00:02:32,090 --> 00:02:35,210 So this is our very basic function called my function. 39 00:02:35,510 --> 00:02:41,750 Now we can extend two functions and in order to do so, I'm going to create a new function so we can 40 00:02:41,750 --> 00:02:43,400 make functions a lot more complex. 41 00:02:43,430 --> 00:02:49,130 For example, I can call one add up, and I'm just going to have two variables here. 42 00:02:49,610 --> 00:02:54,560 I'm going to have one variable, which is of Typekit and another variable, which is also of Typekit, 43 00:02:55,100 --> 00:02:58,130 and it should return a variable of type. 44 00:02:58,940 --> 00:03:00,920 So what do all of these things do? 45 00:03:01,310 --> 00:03:04,100 Well, first of all, we have two parameters here. 46 00:03:04,190 --> 00:03:08,030 So there's this parameter one, which is called a which is of type integer. 47 00:03:08,390 --> 00:03:13,640 And then we have another parameter, which is called B, and it's also a type integer. 48 00:03:14,330 --> 00:03:17,480 So now the key word here is parameter. 49 00:03:17,750 --> 00:03:18,110 All right. 50 00:03:18,110 --> 00:03:19,580 So we have two parameters here. 51 00:03:20,550 --> 00:03:25,490 Then after the colon, we say of which type the return type should be. 52 00:03:25,790 --> 00:03:30,950 So in this case, our function add up is going to return something once we call it. 53 00:03:31,460 --> 00:03:36,560 So instead of just executing some code in there, it's also going to return something. 54 00:03:36,560 --> 00:03:39,890 So it's going to give us something back that we can then reuse. 55 00:03:40,790 --> 00:03:45,590 OK, so the idea here is that we need to use the return keyword. 56 00:03:45,590 --> 00:03:47,420 As you can see, it's complaining here. 57 00:03:47,420 --> 00:03:49,910 So now it's complaining about the returns. 58 00:03:49,910 --> 00:03:51,890 So let's say I always return zero. 59 00:03:52,310 --> 00:03:57,620 Then this add up would always return zero, no matter what I put into it. 60 00:03:58,370 --> 00:04:00,020 So let's change that a little bit. 61 00:04:00,170 --> 00:04:03,050 Let's say I want to actually add up those two values. 62 00:04:03,050 --> 00:04:04,100 So A and B. 63 00:04:04,670 --> 00:04:12,380 So what we will do is we would return a plus B in this case because we want to return whatever A and 64 00:04:12,380 --> 00:04:13,430 B are added up. 65 00:04:13,820 --> 00:04:17,420 Now it's going to return whatever is the result of A-plus B. 66 00:04:17,990 --> 00:04:26,180 So our parameters is the definition of the input that we want to have and then we have an output. 67 00:04:26,570 --> 00:04:28,400 And it's also of type int. 68 00:04:28,790 --> 00:04:33,470 So whatever will return will be our output and it should be of the type that we have defined here. 69 00:04:34,100 --> 00:04:44,330 Parameter input and then return type is our output. 70 00:04:46,070 --> 00:04:52,460 OK, so now we can go ahead and use this function, add up in our main function, so I'm going to get 71 00:04:52,460 --> 00:04:59,660 rid of my function calls here, and I'm just going to call add up and I'm going to add up with five 72 00:04:59,660 --> 00:05:00,500 and three. 73 00:05:00,560 --> 00:05:02,210 So this should return eight. 74 00:05:02,780 --> 00:05:08,900 Now the thing is, it will return eight, but we have no way of knowing that it returns eight, except 75 00:05:08,900 --> 00:05:14,570 for, let's say, we stored in a variable or we use our debugging tools. 76 00:05:14,960 --> 00:05:18,830 But I'm just going to store it than a variable, which I'm going to call result. 77 00:05:19,700 --> 00:05:24,500 And it's going to be the result of whatever add up is going to come up with and it's going to store 78 00:05:24,500 --> 00:05:25,190 it in result. 79 00:05:25,700 --> 00:05:33,250 So now what I'm going to do is I'm just going to use result is and concatenation year result. 80 00:05:34,130 --> 00:05:35,240 So very basic. 81 00:05:35,720 --> 00:05:37,250 And of course, we're getting. 82 00:05:38,190 --> 00:05:41,400 The template requests here, so this is the alternative way. 83 00:05:41,670 --> 00:05:43,080 All right, so the result is results. 84 00:05:43,590 --> 00:05:46,950 And now we can go ahead and test what we're actually going to get. 85 00:05:47,760 --> 00:05:48,810 So there we are. 86 00:05:48,840 --> 00:05:51,900 It says results as late as you can see, it worked. 87 00:05:52,320 --> 00:05:58,110 So the input was five and three and the output was eight. 88 00:05:58,410 --> 00:06:00,600 And that is what we saw as a result. 89 00:06:01,590 --> 00:06:06,600 Now there is something to say about the terms here because here. 90 00:06:08,250 --> 00:06:14,460 What we are using is called parameters, right, and so we are using parameter and parameter beat now 91 00:06:14,460 --> 00:06:22,620 when we pass a value to our function called add up, this is an argument and this is also an argument. 92 00:06:23,340 --> 00:06:26,100 So the term here is argument. 93 00:06:26,730 --> 00:06:27,030 All right. 94 00:06:27,030 --> 00:06:29,850 You're not having an argument here, but we are using arguments. 95 00:06:30,510 --> 00:06:37,830 OK, so the idea is that they are two different terms and this is something that people mix up all the 96 00:06:37,830 --> 00:06:38,190 time. 97 00:06:38,880 --> 00:06:45,990 And yeah, in some cases, they really just ignore the whole concept of parameter, an argument or distinguishing 98 00:06:45,990 --> 00:06:46,710 between the two. 99 00:06:47,010 --> 00:06:54,500 So either parameter or argument is going to be fine when you use it in combination with functions. 100 00:06:54,510 --> 00:07:00,210 OK, but be aware that there is a difference between the term argument and parameter. 101 00:07:00,750 --> 00:07:08,130 So I might even mix them the term term sometimes when talking about function, so please be forgiving. 102 00:07:09,090 --> 00:07:15,870 Then there is another thing about functions and you probably heard the term or maybe heard the term 103 00:07:16,110 --> 00:07:16,770 methods. 104 00:07:17,160 --> 00:07:26,400 So there is this called method, and a method is a function within a class. 105 00:07:26,850 --> 00:07:29,310 So that's the only difference between the two. 106 00:07:29,790 --> 00:07:30,990 Otherwise, they are the same. 107 00:07:31,230 --> 00:07:36,180 So in some programming languages which are not object oriented programming languages, then it's always 108 00:07:36,180 --> 00:07:37,110 going to be a function. 109 00:07:37,110 --> 00:07:43,680 But in programming languages which are object oriented, it's going to be most of the case as a method 110 00:07:44,040 --> 00:07:46,860 because it is defined within a class. 111 00:07:47,670 --> 00:07:51,450 In our case here, however, we're not having a class. 112 00:07:51,450 --> 00:07:53,790 So it's not like in our main activity. 113 00:07:54,330 --> 00:07:59,730 For example, in our class main activity there, we have a function called on create. 114 00:08:00,090 --> 00:08:04,980 But to be more precise, this is a method because it is within a class. 115 00:08:05,280 --> 00:08:07,650 So it is a method here. 116 00:08:08,310 --> 00:08:12,520 There are a bunch of other words that come up in programming languages, right? 117 00:08:12,540 --> 00:08:15,780 And what it really helps is a dictionary sometimes. 118 00:08:15,780 --> 00:08:21,000 So Kotlin programmer dictionary, you can find it under blog Scotland Monice Academy dot com. 119 00:08:21,000 --> 00:08:26,740 So they wrote a little dictionary with the main words here. 120 00:08:26,760 --> 00:08:32,940 So function versus procedure, then function versus method and so forth. 121 00:08:33,780 --> 00:08:36,789 So here are a bunch more field versus property. 122 00:08:36,809 --> 00:08:37,919 What's the difference here? 123 00:08:38,370 --> 00:08:39,690 Parameter versus argument? 124 00:08:39,690 --> 00:08:42,600 That's something that we have seen before and so forth. 125 00:08:43,640 --> 00:08:49,700 So if you want to really dig deep into all of the different terms and terminology, then check out this 126 00:08:49,700 --> 00:08:50,330 article here. 127 00:08:52,410 --> 00:08:52,760 All right. 128 00:08:53,100 --> 00:08:58,830 So coming back to our cut on basics, a little challenge for you, please write another function which 129 00:08:58,830 --> 00:09:03,240 will not add up, but which will build the average of two values. 130 00:09:03,450 --> 00:09:06,090 So here you have to be careful with the data type. 131 00:09:06,480 --> 00:09:10,980 And yeah, just please try to build that function by yourself. 132 00:09:15,270 --> 00:09:15,700 All right. 133 00:09:15,780 --> 00:09:16,980 So let's go ahead. 134 00:09:17,010 --> 00:09:17,850 I hope you tried it. 135 00:09:18,210 --> 00:09:23,700 So here I'm just going to use the funky word and then I'm going to call this one a veggie setting for 136 00:09:23,700 --> 00:09:24,210 average. 137 00:09:24,510 --> 00:09:25,980 And I need two values. 138 00:09:25,980 --> 00:09:29,250 One of them will be a double and the other one should also be a double. 139 00:09:29,490 --> 00:09:37,660 So the thing about those is that the return of an average usually is not a whole number. 140 00:09:37,680 --> 00:09:44,190 So that's why I'm using doubles here, and it's going to be a double as a return type as well here. 141 00:09:45,000 --> 00:09:50,730 And then we can return a plus b divided by two. 142 00:09:52,500 --> 00:09:53,310 That's going to be it. 143 00:09:53,940 --> 00:10:00,810 So now, of course, we can see what the result is, so let's call this one average instead. 144 00:10:01,260 --> 00:10:06,480 And we're going to not call the add up function, but we're going to call the average function. 145 00:10:06,840 --> 00:10:12,630 And I'm just going to say, OK, what's the average of five and 13 and then? 146 00:10:15,170 --> 00:10:22,250 In order to be precise, we need to make those double so five point three and thirteen point thirty 147 00:10:22,250 --> 00:10:22,670 seven. 148 00:10:24,650 --> 00:10:32,150 And no less around us and that we are to as result is nine point thirty three, four or five. 149 00:10:33,200 --> 00:10:33,620 All right. 150 00:10:34,190 --> 00:10:39,230 So these are the basics of functions, so what's the whole point of functions? 151 00:10:39,680 --> 00:10:42,780 Well, there are multiple advantages of functions. 152 00:10:42,830 --> 00:10:45,560 So one of them is that you can reuse code. 153 00:10:46,040 --> 00:10:53,330 Another one is, for example, that you can work with a partner so you take care of one piece of code 154 00:10:53,330 --> 00:10:56,480 and he takes care of another piece of code or she takes care of it. 155 00:10:56,750 --> 00:11:02,310 And then you can just combine them by having functions that you can then reuse. 156 00:11:02,390 --> 00:11:08,660 So you can write your own functions and then you can both use the functions of the other person. 157 00:11:09,080 --> 00:11:15,350 And then another thing is that you can use functions that are prebuilt and you can just use the functionality 158 00:11:15,350 --> 00:11:15,680 of them. 159 00:11:15,950 --> 00:11:17,890 So that's the whole idea of functions. 160 00:11:17,900 --> 00:11:24,710 They add functionality to your program and you can simply use them for whatever purpose you built them 161 00:11:24,710 --> 00:11:26,720 or for whatever purpose they were built. 162 00:11:26,990 --> 00:11:35,120 And there are gazillions of functions out there that you can reuse just by using this import feature 163 00:11:35,120 --> 00:11:35,360 here. 164 00:11:35,690 --> 00:11:40,730 As you can see here, import Android OS bundle well in there, there are a bunch of functions that we 165 00:11:40,730 --> 00:11:41,330 can reuse. 166 00:11:41,330 --> 00:11:47,210 So if you open up the bundled class, for example, you can see that in there you have. 167 00:11:48,990 --> 00:11:49,860 A bunch of. 168 00:11:50,720 --> 00:12:00,200 Constructors, but then we have a maybe pre-filled has f the s function, then we have another function 169 00:12:00,200 --> 00:12:00,860 called. 170 00:12:01,990 --> 00:12:04,720 For Pear, which is going to return a bundle. 171 00:12:05,020 --> 00:12:09,850 Well, you might wonder what all of these public aesthetic bundle keywords mean. 172 00:12:09,850 --> 00:12:12,370 Well, the thing is this is Java coat. 173 00:12:12,430 --> 00:12:13,690 So this is not codling coat. 174 00:12:13,990 --> 00:12:19,120 And in Java, the structure of a function is slightly different than it is in codling. 175 00:12:19,840 --> 00:12:20,200 All right. 176 00:12:20,590 --> 00:12:22,440 But the basic idea is the same. 177 00:12:22,450 --> 00:12:28,840 It's has a return type, then it has a name, and then it has a bunch of parameters here. 178 00:12:29,440 --> 00:12:31,240 Just the order is a little different. 179 00:12:31,480 --> 00:12:35,350 But in the end, what it does, it has a bunch of code that you can then reuse. 180 00:12:36,190 --> 00:12:39,940 So no worries if you don't understand the stuff that is going on in here. 181 00:12:40,300 --> 00:12:42,670 Because for one, it's not Kotlin code. 182 00:12:42,670 --> 00:12:47,470 And the second thing is you don't need to understand everything behind the function in order to use 183 00:12:47,470 --> 00:12:48,040 the function. 184 00:12:48,400 --> 00:12:52,930 Sometimes you just need to have the output right, and that is really powerful. 185 00:12:53,650 --> 00:12:54,820 It's like driving a car. 186 00:12:54,940 --> 00:13:01,870 You don't have to be a mechanic in order to drive a car, or you don't need to be able to repair a car 187 00:13:01,870 --> 00:13:02,950 in order to drive a car. 188 00:13:02,980 --> 00:13:08,770 You can just sit in there and be happy about all of the features that were added to the car and that 189 00:13:08,770 --> 00:13:09,490 you can now use. 190 00:13:10,330 --> 00:13:12,450 All right, so these are the basics of functions. 191 00:13:12,460 --> 00:13:16,990 And yeah, let's check out the next video where we're going to look at knuckleballs. 192 00:13:17,200 --> 00:13:18,070 So see you there.