All language subtitles for 027 Functions - Parameters - Arguments and Return - Exercise Included__en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,180 --> 00:00:00,960 Welcome back. 2 00:00:01,140 --> 00:00:06,350 In this video, we are going to look at functions and functions are something that we have used already, 3 00:00:06,360 --> 00:00:11,760 for example, the main function, and now we're going to see how we can create our own functions, which 4 00:00:11,760 --> 00:00:15,390 we then can use to reuse code very easily. 5 00:00:15,690 --> 00:00:20,730 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,170 a lot clearer if we have everything on one screen. 7 00:00:25,560 --> 00:00:27,030 So we have this main function. 8 00:00:27,030 --> 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,920 --> 00:00:35,880 So if no code isn't here, then nothing will happen if we start the program. 10 00:00:36,180 --> 00:00:39,270 So that's important to know and you know that already. 11 00:00:39,630 --> 00:00:46,290 So the next thing we can do is we can create code outside of this function and then call it within this 12 00:00:46,290 --> 00:00:46,730 function. 13 00:00:46,950 --> 00:00:52,280 And in order to do so, you can just go ahead and create a new function with the keyboard fun. 14 00:00:52,770 --> 00:00:57,420 And then you give this a name and I'm going to call this one my function. 15 00:00:59,260 --> 00:01:04,360 And this is generally the structure that you have, so you have this keyword fun, then you have a name 16 00:01:04,360 --> 00:01:08,600 for the function, you have rounded brackets and then you have curly brackets. 17 00:01:08,740 --> 00:01:14,910 And within the rounded brackets, you can put all your parameters within the curly brackets. 18 00:01:14,920 --> 00:01:19,450 You can add to code block that should be executed once this function is called. 19 00:01:20,020 --> 00:01:23,420 So what do all of these terms mean and what we will see in the second? 20 00:01:23,440 --> 00:01:26,780 So let's get started first with something very basic. 21 00:01:26,800 --> 00:01:34,010 So I'm just going to use print here and I'm just going to say called from my function. 22 00:01:35,200 --> 00:01:35,740 All right. 23 00:01:35,740 --> 00:01:38,020 So now let's just call this my function. 24 00:01:38,020 --> 00:01:39,350 How do we call a function? 25 00:01:39,760 --> 00:01:46,540 Well, we call it by just using the name of the function and then the brackets here. 26 00:01:47,200 --> 00:01:51,860 And if we have any arguments that we want to pass, then we need to pass them as well. 27 00:01:52,480 --> 00:01:55,600 So let's run this code just to see if this is working. 28 00:01:56,750 --> 00:02:00,770 And once you run it, you will see that it's going to say called from my function. 29 00:02:01,340 --> 00:02:06,770 All right, so this print statement that was within my function, so within the function that we called 30 00:02:06,770 --> 00:02:10,860 my function was called once we called this function here. 31 00:02:11,060 --> 00:02:18,020 So if we decided to call this function twice, then it's going to display code for my function twice 32 00:02:18,020 --> 00:02:22,570 here and that we are called for my function and again called from my function. 33 00:02:22,820 --> 00:02:27,030 And as we are using a print statement, they are dialed directly after another. 34 00:02:27,050 --> 00:02:31,620 So there is no space in between or no line break either. 35 00:02:32,090 --> 00:02:35,210 So this is our very basic function called my function. 36 00:02:35,510 --> 00:02:41,990 Now we can extend functions and in order to do so, I'm going to create a new function so we can make 37 00:02:41,990 --> 00:02:43,430 functions a lot more complex. 38 00:02:43,460 --> 00:02:49,180 For example, I can call one add up and I'm just going to have two variables here. 39 00:02:49,610 --> 00:02:55,580 I'm going to have one variable which is of type it and another variable, which is also type int and 40 00:02:55,730 --> 00:02:58,370 it should return a variable of type int. 41 00:02:58,910 --> 00:03:00,920 So what do all of these things do? 42 00:03:01,340 --> 00:03:04,150 Well, first of all, we have two parameters here. 43 00:03:04,190 --> 00:03:08,060 So this is parameter one, which is called a which is of type integer. 44 00:03:08,360 --> 00:03:13,650 And then we have another parameter, which is called B and it's also of type integer. 45 00:03:14,300 --> 00:03:17,450 So now the keyword here is parameter. 46 00:03:17,690 --> 00:03:18,130 All right. 47 00:03:18,140 --> 00:03:19,560 So we have two parameters here. 48 00:03:20,540 --> 00:03:25,490 Then after the colon, we say of which type the return type should be. 49 00:03:25,790 --> 00:03:30,990 So in this case, our function add up is going to return something once we call it. 50 00:03:31,430 --> 00:03:36,570 So instead of just executing some code in there, it's also going to return something. 51 00:03:36,590 --> 00:03:39,950 So it's going to give us something back that we can then reuse. 52 00:03:40,730 --> 00:03:45,590 OK, so the idea here is that we need to use the return key word. 53 00:03:45,590 --> 00:03:47,440 As you can see, it's complaining here. 54 00:03:47,460 --> 00:03:49,930 So now it's complaining about the returns. 55 00:03:49,940 --> 00:03:57,500 So let's say I always return zero, then this add up would always return zero no matter what I put into 56 00:03:57,500 --> 00:03:57,670 it. 57 00:03:58,340 --> 00:04:00,030 So let's change that a little bit. 58 00:04:00,200 --> 00:04:06,830 Let's say I want to actually add up those two values so A and B. So what we will do is we would return 59 00:04:06,830 --> 00:04:13,470 A plus B in this case because we want to return whatever A and B are added up. 60 00:04:13,820 --> 00:04:22,580 Now it's going to return whatever is the result of A plus B, so our parameters is the definition of 61 00:04:22,580 --> 00:04:24,050 the input that we want to have. 62 00:04:24,740 --> 00:04:28,430 And then we have an output and it's also of type int. 63 00:04:28,760 --> 00:04:33,500 So whatever will return will be our output and it should be of the type that we have defined here. 64 00:04:34,100 --> 00:04:44,390 Parameter input and then return type is our output. 65 00:04:46,040 --> 00:04:52,490 OK, so now we can go ahead and use this function, add up in our main function, so I'm going to get 66 00:04:52,490 --> 00:04:59,990 rid of my function calls here and I'm just going to call add up and I'm going to add up with five and 67 00:04:59,990 --> 00:05:00,520 three. 68 00:05:00,560 --> 00:05:02,210 So this should return eight. 69 00:05:02,780 --> 00:05:08,930 Now, the thing is, it will return eight, but we have no way of knowing that it returns eight except 70 00:05:08,930 --> 00:05:14,600 for, let's say we store it in a variable or we use our debugging tools. 71 00:05:14,960 --> 00:05:20,990 But I'm just going to store it in a variable, which I'm going to call result, and it's going to be 72 00:05:20,990 --> 00:05:25,220 the result of whatever add up is going to come up with and it's going to store it in result. 73 00:05:25,670 --> 00:05:33,300 So now what I'm going to do is I'm just going to use result is and concatenation year result. 74 00:05:34,100 --> 00:05:35,270 So very basic. 75 00:05:35,690 --> 00:05:37,280 And of course we're getting. 76 00:05:38,190 --> 00:05:39,970 The template requests here. 77 00:05:39,990 --> 00:05:41,400 So this is the alternative way. 78 00:05:41,580 --> 00:05:46,980 All right, so result is result and now we can go ahead and test what we're actually going to get. 79 00:05:47,730 --> 00:05:48,790 So there we are. 80 00:05:48,870 --> 00:05:51,940 It says, results aside, as you can see, it worked. 81 00:05:52,290 --> 00:05:58,140 So the input was five and three and the output was eight. 82 00:05:58,380 --> 00:06:00,630 And that is what we saw as a result. 83 00:06:01,590 --> 00:06:06,630 Now, there is something to say about the terms here because here. 84 00:06:08,250 --> 00:06:14,760 What we are using is called parameters, right, and so we are using parameter and parameter B now when 85 00:06:14,760 --> 00:06:18,870 we pass a value to our function called add up. 86 00:06:19,470 --> 00:06:22,640 This is an argument and this is also an argument. 87 00:06:23,340 --> 00:06:26,120 So the term here is argument. 88 00:06:26,700 --> 00:06:27,030 All right. 89 00:06:27,040 --> 00:06:29,880 You're not having an argument here, but we are using arguments. 90 00:06:30,530 --> 00:06:37,860 OK, so the idea is that they are two different terms and this is something that people mix up all the 91 00:06:37,860 --> 00:06:38,190 time. 92 00:06:38,850 --> 00:06:45,990 And yeah, in some cases they really just ignore the whole concept of parmeter an argument or distinguishing 93 00:06:45,990 --> 00:06:46,740 between the two. 94 00:06:47,010 --> 00:06:54,480 So either Parmeter or argument is going to be fine when you use it in combination with functions. 95 00:06:54,540 --> 00:07:00,210 OK, but be aware that there is a difference between the term argument and parameter. 96 00:07:00,750 --> 00:07:05,640 So I might even mix them the term terms sometimes when talking about function. 97 00:07:05,680 --> 00:07:09,450 So please be forgiving then. 98 00:07:09,480 --> 00:07:16,790 There is another thing about functions and you probably heard the term or maybe heard the term methods. 99 00:07:17,130 --> 00:07:26,420 So there is this called method and a method is a function within a class. 100 00:07:26,820 --> 00:07:29,340 So that's the only difference between the two. 101 00:07:29,760 --> 00:07:30,990 Otherwise they are the same. 102 00:07:31,230 --> 00:07:36,210 So in some programming languages, which are not object oriented programming languages, then it's always 103 00:07:36,210 --> 00:07:37,140 going to be a function. 104 00:07:37,140 --> 00:07:43,740 But in programming languages which are object oriented, it's going to be most of the case as a method 105 00:07:44,040 --> 00:07:48,600 because it is defined within a class in our case here. 106 00:07:48,630 --> 00:07:55,830 However, we are not having a class, so it's not like in our main activity, for example, in our class 107 00:07:55,830 --> 00:07:59,790 main activity there we have a function called uncreate. 108 00:08:00,090 --> 00:08:05,000 But to be more precise, this is a method because it is within a class. 109 00:08:05,280 --> 00:08:07,650 So it is a method here. 110 00:08:08,340 --> 00:08:12,210 There are a bunch of other words that come up in programming languages. 111 00:08:12,210 --> 00:08:12,570 Right. 112 00:08:12,570 --> 00:08:15,810 And what it really helps is a dictionary sometimes. 113 00:08:15,810 --> 00:08:21,000 So Cutline program, a dictionary, you can find it under plug that cutline minus academy dot com. 114 00:08:21,010 --> 00:08:26,780 So they wrote a little dictionary with the main words here. 115 00:08:26,790 --> 00:08:32,980 So function versus procedure, then function versus method and so forth. 116 00:08:33,750 --> 00:08:36,830 So here are a bunch more field versus property. 117 00:08:36,840 --> 00:08:37,950 What's the difference here? 118 00:08:38,370 --> 00:08:39,720 Parameter versus argument. 119 00:08:39,720 --> 00:08:42,630 That's something that we have seen before and so forth. 120 00:08:43,610 --> 00:08:49,730 So if you want to really dig deep into all of the different terms and terminology, then check out this 121 00:08:49,730 --> 00:08:50,360 article here. 122 00:08:52,350 --> 00:08:58,380 Already, so coming back to our cutline basics, a little challenge for you, please write another function 123 00:08:58,530 --> 00:09:03,280 which will not add up, but which will build the average of two values. 124 00:09:03,420 --> 00:09:06,150 So here you have to be careful with the data type. 125 00:09:06,450 --> 00:09:11,030 And yeah, just please try to build that function by yourself. 126 00:09:15,210 --> 00:09:16,980 All right, so let's go ahead. 127 00:09:17,010 --> 00:09:22,410 I hope you tried it so here I'm just going to use the funky word and then I'm going to call this one 128 00:09:22,410 --> 00:09:24,240 a Viji standing for average. 129 00:09:24,480 --> 00:09:26,010 And I need two values. 130 00:09:26,010 --> 00:09:29,230 One of them will be a double and the other one should also be a double. 131 00:09:29,490 --> 00:09:37,680 So the thing about those is that the return of an average usually is not a whole number. 132 00:09:37,710 --> 00:09:39,260 So that's why I'm using doubles here. 133 00:09:39,930 --> 00:09:44,210 And it's going to be a double as a return type as well here. 134 00:09:44,940 --> 00:09:50,760 And then we can return A plus B divided by two. 135 00:09:52,500 --> 00:09:53,350 That's going to be it. 136 00:09:53,910 --> 00:10:01,980 So now, of course, we can see what the result is, so let's call this one average instead, and we're 137 00:10:01,990 --> 00:10:06,510 going to not call the add up function, but we're going to call the average function. 138 00:10:06,780 --> 00:10:11,280 And I'm just going to say, OK, what's the average of five and thirteen? 139 00:10:12,000 --> 00:10:12,660 And then. 140 00:10:15,140 --> 00:10:22,280 In order to be precise, we need to make those doubles so five point three and thirteen point three 141 00:10:22,280 --> 00:10:32,210 seven and no, let's run this and we are as a result is nine point three, three, four or five. 142 00:10:33,140 --> 00:10:36,930 All righty, so these are the basics of functions. 143 00:10:36,980 --> 00:10:39,260 So what's the whole point of functions? 144 00:10:39,680 --> 00:10:42,820 Well, there are multiple advantages of functions. 145 00:10:42,830 --> 00:10:45,570 So one of them is that you can reuse code. 146 00:10:46,010 --> 00:10:50,320 Another one is, for example, that you can work with a partner. 147 00:10:50,330 --> 00:10:56,390 So you take care of one piece of code and he takes care of another piece of code or she takes care of 148 00:10:56,390 --> 00:10:56,510 it. 149 00:10:56,750 --> 00:11:03,980 And then you can just combine them by having functions that you can then reuse so you can write your 150 00:11:03,980 --> 00:11:08,660 own functions and then you can both use the functions of the other person. 151 00:11:09,050 --> 00:11:15,380 And then another thing is that you can use functions that are pre-built and you can just use the functionality 152 00:11:15,380 --> 00:11:15,700 of them. 153 00:11:15,920 --> 00:11:17,920 So that's the whole idea of functions. 154 00:11:17,930 --> 00:11:24,740 They add functionality to your program and you can simply use them for whatever purpose you built them 155 00:11:24,740 --> 00:11:26,750 or for whatever purpose they were built. 156 00:11:26,990 --> 00:11:35,120 And there are gazillions of functions out there that you can reuse just by using this important feature 157 00:11:35,120 --> 00:11:38,390 here, as you can see here, import Android OS bundle. 158 00:11:38,390 --> 00:11:41,360 Well, in there there are a bunch of functions that we can use. 159 00:11:41,390 --> 00:11:46,070 So if you open up the bundle class, for example, you can see that in there. 160 00:11:46,430 --> 00:11:47,270 You have. 161 00:11:48,960 --> 00:11:49,890 A bunch of. 162 00:11:50,690 --> 00:11:59,750 Constructor's, but then we have a maybe Prefill has left the Earth function, then we have another 163 00:11:59,750 --> 00:12:00,890 function called. 164 00:12:01,960 --> 00:12:04,750 For pair, which is going to return a bundle. 165 00:12:05,020 --> 00:12:09,850 Well, you might wonder what all of these public static bundle keywords mean. 166 00:12:09,880 --> 00:12:12,410 Well, the thing is, this is Java code. 167 00:12:12,460 --> 00:12:13,700 So this is not codling code. 168 00:12:13,960 --> 00:12:19,150 And in Java, the structure of a function is slightly different than it is in Scotland. 169 00:12:19,810 --> 00:12:20,250 All right. 170 00:12:20,560 --> 00:12:22,450 But the basic idea is the same. 171 00:12:22,450 --> 00:12:28,870 It's has a return type, then it has a name and then it has a bunch of parameters here. 172 00:12:29,440 --> 00:12:31,270 Just the order is a little different. 173 00:12:31,490 --> 00:12:35,380 But in the end, what it does, it has a bunch of code that you can then reuse. 174 00:12:36,190 --> 00:12:42,130 So no worries if you don't understand the stuff that is going on in here, because for one, it's not 175 00:12:42,130 --> 00:12:42,720 codling code. 176 00:12:42,730 --> 00:12:47,470 And the second thing is you don't need to understand everything behind the function in order to use 177 00:12:47,470 --> 00:12:48,030 the function. 178 00:12:48,370 --> 00:12:50,260 Sometimes you just need to have the output. 179 00:12:50,260 --> 00:12:50,620 Right. 180 00:12:51,070 --> 00:12:52,930 And that is really powerful. 181 00:12:53,660 --> 00:12:54,850 It's like driving a car. 182 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 183 00:13:01,870 --> 00:13:02,970 in order to drive a car. 184 00:13:02,980 --> 00:13:08,800 You can just sit in there and be happy about all of the features that were added to the car and that 185 00:13:08,800 --> 00:13:09,550 you can now use. 186 00:13:10,270 --> 00:13:10,750 All right. 187 00:13:10,780 --> 00:13:12,480 So these are the basics of functions. 188 00:13:12,490 --> 00:13:17,040 And, yeah, let's check out the next video where we're going to look at knuckleballs. 189 00:13:17,170 --> 00:13:18,100 So see you there. 18637

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.