All language subtitles for 4. Multiple Arguments and Argument Defaults

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: WEBVTT 00:00.270 --> 00:05.460 In this video we're going to continue to explore functions by talking about how we can create functions 00:05.460 --> 00:12.270 with multiple arguments and how we can set default values for arguments that aren't actually provided 00:12.390 --> 00:14.580 when the function is called to do this. 00:14.580 --> 00:17.810 We're going to create a brand new file in the functions directory. 00:18.000 --> 00:19.280 Let's go ahead and call this one. 00:19.290 --> 00:21.170 Something like arguments. 00:21.210 --> 00:21.540 J. 00:21.540 --> 00:25.660 S. And then we're going to go ahead and mess around with three examples. 00:25.740 --> 00:28.770 The first two exploring the features and the last one. 00:28.770 --> 00:30.100 A challenge for you. 00:30.110 --> 00:33.700 We're going to kick things off by creating a function that takes multiple arguments. 00:33.720 --> 00:38.500 It is going to take in multiple numbers add them all together and return the sum. 00:38.520 --> 00:44.680 So if we were going to set that up we could use that to create add and set it equal to right here. 00:44.730 --> 00:50.870 What are we going to set that equal to a function and that function is going to take multiple arguments 00:50.880 --> 00:54.270 but for the moment we'll just put nothing inside of parentheses. 00:54.270 --> 00:58.870 Now if we were to call add and do something with that result it might look a bit like this. 00:58.890 --> 01:05.240 Let result equal a call to add and then we can do something with the result like printed cancel that 01:05.250 --> 01:07.260 log result. 01:07.260 --> 01:12.270 Now this is just a little template for how things might look but it doesn't actually return anything. 01:12.270 --> 01:17.370 So result is going to print undefined which is what we learned about in the last video. 01:17.370 --> 01:21.570 Now let's actually set up the arguments and function body for ADD. 01:21.870 --> 01:24.630 If we wanted a single argument for AD What would we do. 01:24.630 --> 01:26.190 We would just name it right here. 01:26.310 --> 01:29.890 If I was going to call it a for example I would set up a. 01:29.940 --> 01:31.830 And then I could use a down below. 01:31.830 --> 01:33.830 Assuming it was actually passed in. 01:33.990 --> 01:37.470 So for example I could pass in the value 10. 01:37.590 --> 01:41.930 Now if I want to pass in multiple arguments how do I set up the definition right here. 01:42.090 --> 01:49.470 All I do is I separate their names by a comma so comma and people usually add a space for formatting 01:49.470 --> 01:54.860 purposes and then right here I can pick what I want to call the second argument that's provided. 01:54.960 --> 02:01.470 So for example I could call it B and then I could pass in a second value by separating it by a comma. 02:01.470 --> 02:06.860 So right here 10 for the first value and let's use one for the second. 02:07.140 --> 02:12.870 So with this call signature we're going to have a equal 10 and be equal one. 02:12.990 --> 02:18.090 So if I were to add them up and return the value I would expect 11 to come back. 02:18.090 --> 02:22.560 Now we can go ahead and create a variable where we add the two numbers together and then return that 02:22.560 --> 02:23.550 variable. 02:23.550 --> 02:27.780 But you can actually just provide the expression right in line. 02:27.900 --> 02:31.860 So I can return a plus b. 02:31.900 --> 02:35.100 This is going to add a and b together resulting in 11. 02:35.220 --> 02:36.520 It's then going to return it. 02:36.540 --> 02:40.240 And if we run our script we should actually see some printing. 02:40.530 --> 02:44.980 Let's go ahead and save the file and check out what happens over here in the terminal. 02:45.150 --> 02:51.750 I'm going to run it node arguments Dhananjay s when I do I see 11 printing which is exactly what we 02:51.750 --> 02:52.820 were hoping for. 02:53.040 --> 02:58.980 So if we want to provide multiple arguments we just create a comma separated list both in the function 02:58.980 --> 03:06.480 definition and in the call signature where we actually pass values in to the function. 03:06.480 --> 03:07.630 Now we can go further. 03:07.620 --> 03:09.960 There's no reason to stop at two. 03:09.960 --> 03:13.040 You can have as many arguments as your function needs. 03:13.050 --> 03:19.590 So let's say we needed a third one comma with a C afterwards and then we could provide a third value. 03:19.590 --> 03:22.050 For example I could provide five. 03:22.110 --> 03:25.050 Now I would expect the sum to be 16. 03:25.050 --> 03:27.380 Assuming I actually use C right here. 03:27.510 --> 03:30.390 So a plus b plus C.. 03:30.390 --> 03:36.990 The result should be 16 and if I rerun the script using the up arrow and enter and that is exactly what 03:36.990 --> 03:37.720 we get. 03:37.860 --> 03:39.230 Now there's really not a whole lot to it. 03:39.240 --> 03:41.250 You can add as many arguments as you want. 03:41.340 --> 03:46.140 You can have a function with no arguments and there's no rule for when to use which it's really going 03:46.140 --> 03:49.200 to depend on what the function needs. 03:49.200 --> 03:54.270 So if we're creating a function that calculates your income tax we're going to need certain values we 03:54.270 --> 04:00.090 might need one argument for example the amount of income you actually made other functions will be more 04:00.090 --> 04:03.210 arguments other functions will need less. 04:03.210 --> 04:06.090 So we've explored how we can set up multiple arguments. 04:06.090 --> 04:12.300 Now what I want to do is figure out how we can provide a default value if an argument isn't actually 04:12.300 --> 04:12.980 provided. 04:12.990 --> 04:18.800 When the function is called Let's go ahead and explore this with a second example down below right here. 04:19.200 --> 04:23.370 Default arguments add up above at the very top of the file. 04:23.370 --> 04:25.970 I'm just going to leave a little comment about this example here. 04:26.010 --> 04:29.360 We explored multiple arguments. 04:29.370 --> 04:30.060 Awesome. 04:30.390 --> 04:32.220 So how do we use default arguments. 04:32.220 --> 04:33.960 Let's explore with an example. 04:34.020 --> 04:38.030 Let's imagine we're creating a multiplayer video game and there's a leaderboard right. 04:38.040 --> 04:43.150 So all of the players ranked from the people with the most points to the least. 04:43.150 --> 04:48.280 And there's two pieces of information that we're going to need in order to generate the individual row. 04:48.420 --> 04:52.230 We're going to need the users name and their score. 04:52.260 --> 04:55.890 Now not everyone is going to have a score that you haven't played a game yet. 04:55.890 --> 05:01.800 We're going to come up with a good default value will use 0 and not everyone is going to provide a name. 05:01.830 --> 05:03.550 Maybe you want to remain anonymous. 05:03.600 --> 05:06.460 So we're going to come up with a default name as well. 05:06.690 --> 05:08.680 Let's explore what this might look like. 05:08.700 --> 05:10.180 We're still going to be making a function. 05:10.200 --> 05:11.330 I'm going to call this one. 05:11.340 --> 05:14.180 Get score taxed. 05:14.220 --> 05:19.500 So this would just be one part of our video game and I'm going to set it equal to a function and this 05:19.500 --> 05:22.070 function is going to take two arguments. 05:22.230 --> 05:26.280 The name of the player and the player's score. 05:26.550 --> 05:32.250 Then down below in the function body we're going to go ahead and actually use these two values to generate 05:32.370 --> 05:34.530 a string before we do though. 05:34.560 --> 05:40.190 Let's just use console dot log to print out name that on the next line. 05:40.200 --> 05:41.590 Cancel that line. 05:41.730 --> 05:43.340 To print out score. 05:43.530 --> 05:47.380 And what we're going to do is call the function down below. 05:47.670 --> 05:53.540 I'm going to call get score taxed and I'm going to provide no values. 05:53.550 --> 05:57.880 I'm going to call it just like that's based off of what we learned in the last video. 05:57.960 --> 06:04.290 I know that name and score will both be undefined and if I rerun the file I would expect to see undefined 06:04.470 --> 06:05.250 twice. 06:05.280 --> 06:07.650 I'm going to rerun things over inside of the terminal. 06:07.650 --> 06:08.610 And what do I get. 06:08.610 --> 06:13.740 I get 16 from the first example and undefined twice from the second example. 06:13.740 --> 06:20.910 So by default if we do not provide an argument value nothing is going to show up but we can actually 06:20.910 --> 06:25.730 set up defaults and we do that right here in our function definition. 06:25.740 --> 06:29.200 So when I define that the second argument should be called score. 06:29.220 --> 06:32.200 I can also provide a default value for that. 06:32.220 --> 06:37.550 So if it isn't provided use this in this case we can set it equal to the number zero. 06:37.590 --> 06:38.460 How do we do that. 06:38.580 --> 06:44.260 Well after the argument name we use space equals space and we give it a value. 06:44.280 --> 06:46.290 So the value could be a string or a boolean. 06:46.290 --> 06:51.450 Anything in Javascript in our case it makes sense to use a number since we're talking about a score. 06:51.600 --> 06:54.180 So I'm going to zero right in here. 06:54.180 --> 06:57.340 Now we could say that score has a default value. 06:57.570 --> 07:03.290 If we change nothing about our function call and rerun the program we're no longer going to see undefine 07:03.480 --> 07:05.200 actually going to see zero. 07:05.400 --> 07:06.640 So what happens here. 07:06.750 --> 07:08.040 The function executes. 07:08.040 --> 07:10.410 It says all right call that second one score. 07:10.410 --> 07:12.520 There is no value so use the default. 07:12.540 --> 07:14.210 And that is what prints. 07:14.310 --> 07:16.860 Now should we provide a value in the call. 07:16.860 --> 07:18.300 It would be used. 07:18.300 --> 07:25.050 So for example if I set the name equal to Andrew as the first argument and the score equal to 100 100 07:25.200 --> 07:27.080 is going to get used instead of zero. 07:27.180 --> 07:32.370 This value only gets used if there is no provided argument value. 07:32.370 --> 07:37.060 So in this case I would expect to see Andrew and 100 and that's exactly what I get. 07:37.080 --> 07:41.240 We overrode the default value because we actually provided one. 07:41.430 --> 07:44.200 Now we could do the same thing for the first argument as well. 07:44.220 --> 07:50.090 I could set a default value for this so after the argument name space equals space. 07:50.100 --> 07:54.220 And in this case we're going to provide a string value as the default value. 07:54.360 --> 08:00.360 So we can open and close some strings using single quotes and right inside I can provide that value 08:00.600 --> 08:02.330 and I'm going to call players with no name. 08:02.340 --> 08:05.930 Anonymous excellent. 08:06.000 --> 08:08.240 Now we have default values for both. 08:08.340 --> 08:10.900 And this makes the function a little more robust. 08:10.920 --> 08:16.640 We could provide no values we could provide all of them or we could provide some combination in between. 08:16.920 --> 08:21.720 So let's go ahead and delete all of the values we pass into the function and just make sure our other 08:21.720 --> 08:24.200 default is working right here. 08:24.810 --> 08:25.800 We run things. 08:25.800 --> 08:30.780 I get anonymous and I get zero with the default in place. 08:30.870 --> 08:36.630 We can actually work on making our function do something meaningful with these two pieces of information. 08:36.630 --> 08:37.990 So in the end of the day get score. 08:37.990 --> 08:42.890 Text is going to return the score text and I'm going to assign it to a variable. 08:42.930 --> 08:51.310 Let score text equal whatever comes back from calling get scored text then I'll print the con.. 08:51.640 --> 08:53.670 Lague score. 08:53.700 --> 08:54.540 Text. 08:54.600 --> 09:00.060 Now all we need to do is to actually return a string from this function as opposed to just seeing what 09:00.060 --> 09:01.850 the argument values are. 09:02.010 --> 09:04.710 So I'm going to delete those canceled out log calls. 09:04.740 --> 09:07.570 Right here we can return a new string. 09:07.800 --> 09:11.790 So for this one this is going to be the individual line in our leader board. 09:11.790 --> 09:18.030 I'll start off with the name calling space and then I'm going to concatenate on the name whether it's 09:18.030 --> 09:22.730 the default value Anonymous or a value actually provided when the function was called. 09:22.770 --> 09:26.430 Right here though either way we get access to that via name. 09:26.430 --> 09:27.380 So that's the first part. 09:27.390 --> 09:34.950 And the second part I'm going to concatenate on a space then score call in space and then their score. 09:34.950 --> 09:37.740 So right here plus score. 09:37.860 --> 09:39.870 Now we're going to get a useful string back. 09:39.870 --> 09:44.700 And when we call the function and print it we should see our little string with the values provided 09:44.700 --> 09:49.560 in this case default values for both since none are passed in to the function call. 09:49.560 --> 09:55.930 I'm going to rerun things from the terminal and from here we get just that name Anonymous score zero. 09:55.950 --> 10:00.600 We could actually put a another little hyphen maybe right in between to make things a little easier 10:00.600 --> 10:01.990 to read. 10:02.010 --> 10:02.720 Awesome. 10:03.060 --> 10:05.440 Now let's go ahead and provide values. 10:05.520 --> 10:11.670 So I am going to provide a name Andrew and will say that I haven't played a game yet so I'll leave a 10:11.670 --> 10:14.160 score out of the function call. 10:14.190 --> 10:16.390 That means the default value is going to be used. 10:16.380 --> 10:21.900 And if I save the file and rerun it I get name Andrew and I get score 0. 10:22.170 --> 10:23.800 We could also do the opposite. 10:23.820 --> 10:27.180 I could say there is no name but they do have a score. 10:27.180 --> 10:32.430 Now if I want to provide the second argument but I don't want to provide the first what I have to do 10:32.520 --> 10:35.240 is use undefined right here. 10:35.280 --> 10:39.210 So undefined comma followed by that second value. 10:39.330 --> 10:42.100 Let's go ahead and say I have a score of ninety nine. 10:42.150 --> 10:48.690 Now I would get name Anonymous and score ninety nine so he can provide no value for other arguments. 10:48.690 --> 10:50.830 We just use undefined right there. 10:50.920 --> 10:55.650 And I remember the only reason I'm doing this is because I want to get access to the second argument 10:55.680 --> 10:57.650 and actually assign a value. 10:57.690 --> 10:59.920 So I do have to provide something for the first one. 10:59.970 --> 11:05.140 If I left things like this looks like this what would happen. 11:05.310 --> 11:09.230 I would have 99 for the name by providing undefined. 11:09.270 --> 11:12.870 I make sure that ninety nine get set as the score. 11:13.140 --> 11:13.860 Perfect. 11:13.860 --> 11:16.050 Now it's Challenge time down below. 11:16.080 --> 11:22.350 Let's create a little challenge area and what you're going to do is create a new function that has multiple 11:22.350 --> 11:25.020 arguments and uses the default arguments. 11:25.020 --> 11:29.340 We just explored this little function is going to be a tip calculator. 11:29.340 --> 11:33.750 So you could call the function something like get tip that's an action that we go to a restaurant and 11:33.750 --> 11:36.630 we spend 100 bucks and we want to tip 20 percent. 11:36.630 --> 11:39.650 What is that number that would be a 20 dollar tip. 11:39.660 --> 11:42.820 We're going to create a little function that figures that out for you. 11:42.960 --> 11:46.100 So right here the function is going to take two arguments. 11:46.110 --> 11:51.290 It is going to take the total for the bill and it's going to take the percentage you want to tip. 11:51.300 --> 11:54.400 You could call this like tip percent for example. 11:54.900 --> 11:57.540 Now for the total It doesn't really make sense to have a default. 11:57.540 --> 12:01.280 Each bill is going to be different depending on what you ordered but for the tip. 12:01.350 --> 12:04.850 We could go ahead and come up with a default tip percentage. 12:04.920 --> 12:10.200 We could say 20 percent for example and we're going to reflect this as a decimal point. 12:10.290 --> 12:12.630 So point five would be a 50 percent tip. 12:12.630 --> 12:17.450 Point one would be a 10 percent tip or point to for a 20 percent tip. 12:17.460 --> 12:23.820 We take the total 100 we multiply it by point two that is going to result in 20 which is the correct 12:23.880 --> 12:24.980 tip value. 12:25.140 --> 12:31.010 All you're going to do is run that calculation return the value that's going to be it for the function. 12:31.140 --> 12:36.700 So two arguments a default for one of them then I wanted to go ahead and actually use it. 12:36.780 --> 12:41.250 Call the function a couple of times with different values for the total and the tip. 12:41.250 --> 12:46.830 Make sure that the default 10 percent shows up and make sure that if you provide a different one like 12:46.830 --> 12:49.970 point three that actually gets used. 12:50.010 --> 12:53.570 Take as much time as you need to create this function and mess around with it. 12:53.670 --> 12:58.470 If you get stuck trying to work through your problem and then we're going to come back and go through 12:58.470 --> 13:02.450 things together paused the video and when you're done click play 13:06.660 --> 13:11.070 all right how that one go I'm going to kick things off by creating my function and I'm going to do that 13:11.080 --> 13:11.970 right here. 13:11.970 --> 13:17.760 Let me get tip equal a function. 13:17.760 --> 13:20.170 This function is going to have two arguments. 13:20.280 --> 13:22.470 We'll provide those in just a second right here. 13:22.530 --> 13:26.070 The first one is going to be the total for the bill. 13:26.100 --> 13:28.080 Once again no default for this one. 13:28.080 --> 13:29.630 Each one is going to be different. 13:29.640 --> 13:35.130 The second one is going to be the tip percent and that's OK if you call these things something else. 13:35.130 --> 13:40.680 That's not the important part the important part is the actual function setup itself. 13:40.680 --> 13:44.040 Now for the tip percent we are going to come up with a default value. 13:44.160 --> 13:47.000 I'm going to set it equal to point two. 13:47.010 --> 13:53.280 So by default you can just provide the total and we will calculate the tip based off of that 20 percent 13:53.490 --> 13:55.240 number inside of it. 13:55.260 --> 13:56.490 What do we need to do. 13:56.520 --> 14:03.660 We just need to multiply the two values together and return the result so I can return total. 14:03.960 --> 14:07.530 Times tip per cent perfect. 14:07.530 --> 14:10.950 Now down below we can actually use that function. 14:10.950 --> 14:18.480 So right here I am going to let a tip equal and then I'm going to call it call get to it and I'm going 14:18.480 --> 14:21.150 to pass in a total let's say 100. 14:21.150 --> 14:26.520 Using that example I came up with before and for this one I'm not going to provide a tip percent value. 14:26.520 --> 14:32.510 So it should use the default point to right here cancel dialog tip. 14:32.850 --> 14:36.500 I'm going to remove the comment and then I'm going to actually run the file. 14:36.630 --> 14:38.750 Let's save things and execute them. 14:39.030 --> 14:45.120 I'm going to use clear to clear the terminal output and the up arrow key twice to rerun the arguments 14:45.180 --> 14:50.810 file right here what do I get for the tip I get 20 which is indeed correct. 14:50.810 --> 14:54.800 Now let's override that and provide a different type percent. 14:54.840 --> 14:57.540 I'm going to provide pointer to five. 14:57.550 --> 14:59.110 A 25 percent test. 14:59.160 --> 15:05.850 And now if I rerun things what do I get right here I get 25 bucks and if I were to change the input 15:05.850 --> 15:12.480 value selling it to something like 40 I would get 25 percent of 40 which would be 10 bucks and if I 15:12.480 --> 15:14.960 rerun it that's exactly what we get. 15:15.210 --> 15:20.340 So real world functions aren't just going to have a single argument like we saw in the past real world 15:20.340 --> 15:22.820 functions are going to take multiple arguments. 15:22.830 --> 15:26.200 There are certain values like total that we're going to require. 15:26.220 --> 15:31.380 There are others like 10 percent where it does make sense to have a default value and are going to get 15:31.380 --> 15:36.840 a lot of experience with this as we actually use functions in the projects we create. 15:36.840 --> 15:41.700 Now that we know this though we are ready to continue learning about functions and we're going to do 15:41.700 --> 15:43.500 just that in the next video. 15:43.500 --> 15:44.350 Stay tuned. 15:44.430 --> 15:45.930 I'll see you then. 21618

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