1 00:00:00,700 --> 00:00:01,330 Welcome back. 2 00:00:01,360 --> 00:00:06,490 Today, we going to talk about JavaScript functions, how we can declare war on, how we can call one, 3 00:00:06,820 --> 00:00:10,870 how we can execute a function in what function expression means. 4 00:00:11,360 --> 00:00:13,260 OK, let's start working. 5 00:00:13,540 --> 00:00:22,840 So create a lecture five file inside, put a node call and call it JavaScript function. 6 00:00:24,460 --> 00:00:29,840 Now, also here, make sure that you linking that file to external file. 7 00:00:29,860 --> 00:00:30,770 That's why we need to link. 8 00:00:31,240 --> 00:00:34,050 Now we have used two functions so far. 9 00:00:34,510 --> 00:00:41,580 The first one was alert in the second one was prompt. 10 00:00:42,370 --> 00:00:43,990 Actually, this was the first one. 11 00:00:44,000 --> 00:00:44,850 This was the second one. 12 00:00:44,860 --> 00:00:45,830 But that doesn't matter. 13 00:00:46,210 --> 00:00:55,240 So if we go to a browser and we type prompt and if you hit enter just like this, don't put the brackets, 14 00:00:55,450 --> 00:00:58,510 it will say it's a function if you put alert. 15 00:00:59,800 --> 00:01:01,300 Oops, Leard. 16 00:01:02,450 --> 00:01:07,190 It's F stands for function, but what are these brackets, let's use them. 17 00:01:08,180 --> 00:01:12,870 Wow, now you see that we are calling and executing the function, right? 18 00:01:13,100 --> 00:01:14,430 That is pretty much it. 19 00:01:14,600 --> 00:01:20,270 So these oops, sorry, these let me just show you this. 20 00:01:20,570 --> 00:01:25,190 These brackets means call and execute the function. 21 00:01:25,520 --> 00:01:29,090 And we usually do them like this. 22 00:01:29,240 --> 00:01:37,490 That means end of an expression, because when we are one to end expression as explained, if you don't 23 00:01:37,490 --> 00:01:44,210 know, just watch the previous lecture or two lectures, I'm not sure, but just explain what the expression 24 00:01:44,210 --> 00:01:45,590 means and why we need to end it. 25 00:01:46,090 --> 00:01:49,790 OK, but we are not going to use this inbuilt functions. 26 00:01:49,790 --> 00:01:50,970 We're going to create our own. 27 00:01:51,170 --> 00:01:54,050 So let's create our first own function in. 28 00:01:54,050 --> 00:01:55,220 Let's declare it. 29 00:01:56,750 --> 00:02:00,600 Clear a function, so F stands for function. 30 00:02:00,620 --> 00:02:03,650 It's a shortcut, so we need to use the key word function. 31 00:02:04,340 --> 00:02:07,130 That's the Goldway my first. 32 00:02:07,990 --> 00:02:15,310 Function and this is my name, I can name it the functions that I want, you can put here my 100 my 33 00:02:15,310 --> 00:02:17,770 my this is my thousand function. 34 00:02:17,770 --> 00:02:21,640 Whatever you want to be here, my name, whatever you makes you happy. 35 00:02:21,880 --> 00:02:23,890 But I just put it here because it makes sense. 36 00:02:24,430 --> 00:02:30,010 Now, in order to know that it's a function, we need to use this parentheses and this curly brackets 37 00:02:30,010 --> 00:02:33,280 means a block of code will go here. 38 00:02:33,520 --> 00:02:41,560 Now console the lock I'm going to use here and I will say so you're going to say hello from here. 39 00:02:42,720 --> 00:02:44,880 Save it now if you go back. 40 00:02:47,060 --> 00:02:48,520 I need to use this. 41 00:02:48,620 --> 00:02:52,440 If you go back, refresh here, you're not going to see the result in. 42 00:02:52,480 --> 00:02:53,090 Why is this? 43 00:02:53,430 --> 00:02:56,090 Well, because we need to call it function. 44 00:02:56,360 --> 00:02:58,370 So we just declared a function. 45 00:02:58,370 --> 00:03:05,420 But we now need to call the function by calling the function with these brackets. 46 00:03:05,420 --> 00:03:08,180 It will actually mean call the function and execute. 47 00:03:08,240 --> 00:03:09,250 Now go back. 48 00:03:09,740 --> 00:03:10,520 There you go. 49 00:03:12,300 --> 00:03:13,660 That is pretty much it. 50 00:03:13,850 --> 00:03:20,060 Now, what is more interesting, a function can have one or more arguments. 51 00:03:20,970 --> 00:03:24,240 Now you will see that this is mixed up with parameters. 52 00:03:26,300 --> 00:03:27,900 Uh, spelled it right, right. 53 00:03:28,100 --> 00:03:30,980 So arguments and parameters, but. 54 00:03:31,870 --> 00:03:39,310 You don't have to worry about if you mixed these as well, because in old tutorials or books, you will 55 00:03:39,310 --> 00:03:41,700 see that they used arguments and parameters. 56 00:03:41,710 --> 00:03:48,180 But I want you to know the read the reality so functions can take arguments in what are the arguments? 57 00:03:48,430 --> 00:03:49,770 Well, we already use them. 58 00:03:51,340 --> 00:03:57,370 These were the arguments for the alert alert rackets and high. 59 00:03:58,360 --> 00:04:04,960 So it would it take one argument that this string this is this is and maybe I didn't tell you back then, 60 00:04:04,960 --> 00:04:09,040 but because we haven't done the functions now there is the time. 61 00:04:09,220 --> 00:04:11,410 So I'm going to create a second function called. 62 00:04:13,540 --> 00:04:25,810 My second, actually, let's put it like this function argument function, and you can put any name 63 00:04:25,810 --> 00:04:31,030 meaningful that you want in here, we will see first argument. 64 00:04:32,240 --> 00:04:33,050 Very good. 65 00:04:34,570 --> 00:04:38,630 Now, you can give any name of the argument and let's say control that log. 66 00:04:39,950 --> 00:04:41,580 The argument only I'm just. 67 00:04:43,740 --> 00:04:48,720 First argument, whatever we passing to the function, that's how you call it, you passing an argument 68 00:04:48,720 --> 00:04:50,450 to a function you're going to control. 69 00:04:50,700 --> 00:04:52,080 So I'm going to just print it out. 70 00:04:52,320 --> 00:04:53,470 So what are we going to do now? 71 00:04:53,520 --> 00:04:54,560 Argument function. 72 00:04:54,570 --> 00:04:55,190 Very good. 73 00:04:55,410 --> 00:04:58,320 And now we need to pass that one so it can be screened. 74 00:04:58,530 --> 00:05:04,710 It can be boolean, it can be any data that you JavaScript type that we have encountered so far. 75 00:05:04,860 --> 00:05:07,020 But I'm going to say hello. 76 00:05:08,990 --> 00:05:11,480 I'm just going to pass a screen and I'm going to save it. 77 00:05:12,720 --> 00:05:13,720 In there you go. 78 00:05:14,070 --> 00:05:18,230 This is the helo that I passed to the function as an argument. 79 00:05:18,540 --> 00:05:26,030 So even if I changed this to just a but I need to call it here as well, A, that it will still work. 80 00:05:26,460 --> 00:05:27,690 This is irrelevant. 81 00:05:27,690 --> 00:05:31,800 What we put here, every name we put here, it's irrelevant. 82 00:05:31,800 --> 00:05:34,210 But what is relevant, what we are passing to function. 83 00:05:34,980 --> 00:05:38,300 Now, this is one argument function, whatever. 84 00:05:38,310 --> 00:05:40,560 What if we have more arguments? 85 00:05:40,920 --> 00:05:44,790 Let's create a function that will take multiple arguments. 86 00:05:44,790 --> 00:05:49,760 For example, let's create a function that will calculate the year that we are born. 87 00:05:49,800 --> 00:05:50,670 So function. 88 00:05:51,950 --> 00:05:52,400 Bert. 89 00:05:54,440 --> 00:06:03,410 Birth year and coup, you can name it whatever you want, first parameter will be your age and now inside 90 00:06:03,410 --> 00:06:05,500 I will see your birth year 91 00:06:08,000 --> 00:06:08,570 again. 92 00:06:08,570 --> 00:06:11,060 I'm naming variable as I want. 93 00:06:11,060 --> 00:06:12,390 You can just change the name. 94 00:06:12,710 --> 00:06:14,810 Now, I just want to say a year minus age. 95 00:06:15,230 --> 00:06:19,790 So whatever we passing to the function here, when we call it, we're. 96 00:06:21,310 --> 00:06:28,060 Here and here, we can pass directly 20, 20 to say 23, cool. 97 00:06:28,630 --> 00:06:33,750 Now the Bertier will get the result of 20, 20 to minus at the age. 98 00:06:33,940 --> 00:06:34,600 Nice. 99 00:06:35,470 --> 00:06:37,100 So console at last. 100 00:06:37,240 --> 00:06:38,680 The birth year. 101 00:06:40,670 --> 00:06:41,240 Save it. 102 00:06:42,840 --> 00:06:43,500 Very good. 103 00:06:43,530 --> 00:06:44,740 We do have the result. 104 00:06:45,450 --> 00:06:49,530 One more thing that I want to show you is that, oh, actually, I didn't see this. 105 00:06:49,770 --> 00:06:52,770 When we have multiple arguments, we need to space them with comma. 106 00:06:52,890 --> 00:06:53,610 Same as here. 107 00:06:54,930 --> 00:06:55,620 Very good. 108 00:06:55,650 --> 00:06:59,130 Now, what is function expression? 109 00:07:00,720 --> 00:07:03,580 And this is very important for you to understand. 110 00:07:03,900 --> 00:07:10,290 Well, let's create a variable called function expression, and that will be equal to a function. 111 00:07:10,620 --> 00:07:11,310 Oops. 112 00:07:13,490 --> 00:07:15,050 And comes along. 113 00:07:16,150 --> 00:07:16,840 Function. 114 00:07:18,640 --> 00:07:19,090 Here. 115 00:07:20,170 --> 00:07:27,520 Now, this is coming from function expression, very good. 116 00:07:29,160 --> 00:07:31,360 Now, what is happening here? 117 00:07:31,870 --> 00:07:33,840 Well, we declare a variable. 118 00:07:34,880 --> 00:07:43,190 And this equal sign, it's not the same as Inmet, this equal sign means these equal means assign a 119 00:07:43,200 --> 00:07:44,960 value to something, right? 120 00:07:45,170 --> 00:07:50,530 So whatever this function returns, we will assign it as a value of this function. 121 00:07:50,810 --> 00:07:53,650 But now dysfunction doesn't have a name. 122 00:07:53,660 --> 00:07:55,580 It's called anonymous function. 123 00:07:55,970 --> 00:08:00,160 Now, the function that they don't have any name, it's called anonymous function. 124 00:08:00,170 --> 00:08:02,540 And this is the beauty of function expression. 125 00:08:02,900 --> 00:08:08,000 Now, there are examples where you can see here we can declare the name, but I'm not going to do that 126 00:08:08,000 --> 00:08:12,140 because those examples are very rare and you don't need them. 127 00:08:12,360 --> 00:08:15,320 OK, so how are we going to call a function without name? 128 00:08:15,350 --> 00:08:17,720 Well, we can use the variable name. 129 00:08:17,720 --> 00:08:19,100 How cool is this? 130 00:08:20,560 --> 00:08:22,700 I'm in the mood today, and that's it. 131 00:08:22,720 --> 00:08:26,860 We don't have any arguments, but we can have one or two or many arguments here as well. 132 00:08:27,010 --> 00:08:28,050 Same as the other ones. 133 00:08:28,960 --> 00:08:29,630 Let's see. 134 00:08:29,650 --> 00:08:31,110 Save it, boom. 135 00:08:31,750 --> 00:08:32,290 There you go. 136 00:08:32,290 --> 00:08:33,070 The cons a lot. 137 00:08:35,000 --> 00:08:35,540 Nice. 138 00:08:35,730 --> 00:08:41,450 So one more thing that I want to show you two more things that I want to share with you tonight and 139 00:08:41,450 --> 00:08:45,200 is that that we are at the moment consoling the output. 140 00:08:46,240 --> 00:08:53,510 And if we remove this one, this nineteen ninety nine, it will be gone, it's nothing, dear. 141 00:08:53,540 --> 00:08:54,340 And why is this? 142 00:08:54,610 --> 00:09:01,150 While console lock is just for us, we printing the values here to be able to see it so the user can 143 00:09:01,150 --> 00:09:01,630 see it. 144 00:09:02,080 --> 00:09:06,860 But the result of this function is stored nowhere. 145 00:09:07,000 --> 00:09:07,430 Right. 146 00:09:08,020 --> 00:09:09,340 So what's the solution? 147 00:09:09,680 --> 00:09:14,140 OK, I'm going to copy this one scan uncommon. 148 00:09:14,150 --> 00:09:14,560 This one. 149 00:09:17,300 --> 00:09:27,740 Return, I'm going to use return nouse, so Bourdier, one word here, one in here, instead of calling 150 00:09:27,740 --> 00:09:32,580 lot, I'm just going to say return the bird here. 151 00:09:32,900 --> 00:09:35,540 So what do you think it will happen now? 152 00:09:35,570 --> 00:09:41,320 When I call this function here with these two arguments, it will go inside, it will do its job, do 153 00:09:41,330 --> 00:09:45,800 it calculation and it will return the result of this, whatever it's taught here. 154 00:09:46,550 --> 00:09:51,080 But how I'm going to access it outside, because if I do it right now, there's no another nineteen 155 00:09:51,080 --> 00:09:57,230 ninety nine and I want so these Bertier actually now can be canceled. 156 00:09:59,780 --> 00:10:00,830 Look again. 157 00:10:02,140 --> 00:10:03,010 That's the one way. 158 00:10:04,300 --> 00:10:04,840 It's. 159 00:10:06,570 --> 00:10:13,800 My hands are not working tonight, so there you go, but this is no different than it was here, right? 160 00:10:14,100 --> 00:10:15,910 We can be better than this. 161 00:10:15,930 --> 00:10:16,590 Of course. 162 00:10:16,800 --> 00:10:18,390 So I'm just going to comment on this one. 163 00:10:19,350 --> 00:10:21,060 But this bad boy here. 164 00:10:21,420 --> 00:10:29,940 So I'm going to see more result or I'm just going to say birth year needs more meaningful like that. 165 00:10:30,180 --> 00:10:31,920 And I'm just going to put here the function. 166 00:10:31,920 --> 00:10:34,200 I'm going to call the function with the two parameters. 167 00:10:35,290 --> 00:10:36,880 Let's make it 20, 23. 168 00:10:38,710 --> 00:10:48,230 So that will return a different year and now we need to cancel a log this birth year because the result. 169 00:10:49,060 --> 00:10:51,170 Oh, now I do have a problem. 170 00:10:51,370 --> 00:10:54,980 See, I have used bird ear and bird ear as a function. 171 00:10:55,420 --> 00:10:56,730 Now, we can't do that. 172 00:10:57,130 --> 00:10:59,080 So bird ear result. 173 00:11:00,460 --> 00:11:03,670 We can't call a function and the variable. 174 00:11:03,670 --> 00:11:04,390 Same name. 175 00:11:06,330 --> 00:11:12,780 Two thousand nights, it's our function is working, so in this budget year result, we have stored 176 00:11:12,780 --> 00:11:15,900 the value of dysfunction and we can use it now. 177 00:11:15,910 --> 00:11:26,580 For example, if I want to subtract one year minus one year, make a breathing space, there it is so 178 00:11:26,580 --> 00:11:27,240 we can reuse it. 179 00:11:27,240 --> 00:11:29,470 That's called code reusability. 180 00:11:29,940 --> 00:11:32,190 Now, return is a key word and it's. 181 00:11:33,430 --> 00:11:39,880 We can't use it as a variable, so even we have this different color, it's a purple for the function 182 00:11:39,880 --> 00:11:45,140 and for the variables, we also have a little bit different color and for the functions name. 183 00:11:45,430 --> 00:11:45,890 Nice. 184 00:11:46,570 --> 00:11:51,780 So this is predefine key keyword that we're not going to be able to use. 185 00:11:52,060 --> 00:11:52,900 It's taken. 186 00:11:52,990 --> 00:11:53,400 Right. 187 00:11:53,710 --> 00:11:54,340 Nice. 188 00:11:54,400 --> 00:11:56,050 So let's do one more thing. 189 00:11:56,260 --> 00:11:57,220 And this is a final. 190 00:11:57,220 --> 00:11:57,770 I promise. 191 00:11:58,030 --> 00:12:00,850 So let's create a function that will take four parameters. 192 00:12:00,940 --> 00:12:01,620 Nice. 193 00:12:01,750 --> 00:12:06,790 Now we are talking, but we will use this Bergère function inside the function. 194 00:12:06,800 --> 00:12:07,690 So that's mean. 195 00:12:07,930 --> 00:12:12,730 We can call the function from a function so we don't need this. 196 00:12:13,030 --> 00:12:15,730 So function name the new function. 197 00:12:15,730 --> 00:12:20,700 Name will be print details. 198 00:12:20,710 --> 00:12:21,480 Nice. 199 00:12:21,490 --> 00:12:23,800 This is good function name. 200 00:12:24,010 --> 00:12:30,900 So it will take first name as an argument, last name and year and age. 201 00:12:30,910 --> 00:12:35,470 Now this year and age we will use them and we will supply to this function. 202 00:12:35,680 --> 00:12:42,490 But the first two that's actually been these days, it's so consulate lock we will use for these print 203 00:12:42,550 --> 00:12:43,050 details. 204 00:12:43,510 --> 00:12:54,130 So my first name is we're going to read Spazz there plus first name and what I haven't shown you what 205 00:12:54,130 --> 00:12:57,370 we can do a new line just like this. 206 00:12:58,950 --> 00:13:06,510 Now, compare this to more times, my last name is Make sure you change here, the last name, this 207 00:13:06,510 --> 00:13:08,540 variable, whatever we pass as an argument. 208 00:13:09,240 --> 00:13:10,080 And now. 209 00:13:12,640 --> 00:13:22,030 I am born oops in and we can say here we can call the function birth year that's called the function 210 00:13:22,030 --> 00:13:27,220 birth year one and pass year and age nice. 211 00:13:28,640 --> 00:13:29,160 Save it. 212 00:13:29,480 --> 00:13:36,440 So what what else we need to do is just to call print details and now you see how these two functions 213 00:13:36,440 --> 00:13:36,900 will work. 214 00:13:37,190 --> 00:13:43,100 So this function will start doing these line, then the second line and it will stop here and it will 215 00:13:43,100 --> 00:13:45,010 do the calculation of this. 216 00:13:45,020 --> 00:13:49,790 OK, so we need to just pass in some details with your details if you want. 217 00:13:50,330 --> 00:13:52,420 So don't put my right. 218 00:13:52,430 --> 00:13:53,130 It's not good. 219 00:13:53,870 --> 00:13:55,670 Make sure that you put in your own details. 220 00:13:55,940 --> 00:14:01,000 And we are just putting twenty, twenty two here and thirty three and save it. 221 00:14:01,190 --> 00:14:02,390 And let's see. 222 00:14:02,450 --> 00:14:03,220 There you go. 223 00:14:03,710 --> 00:14:04,670 This is the result. 224 00:14:04,680 --> 00:14:07,660 So now you know how to call function from inside a function. 225 00:14:08,090 --> 00:14:10,280 So I will see you in the next one. 226 00:14:10,280 --> 00:14:11,780 And we are done for tonight.