Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In this video we're going to continue to explore functions by talking about how we can create functions
with multiple arguments and how we can set default values for arguments that aren't actually provided
when the function is called to do this.
We're going to create a brand new file in the functions directory.
Let's go ahead and call this one.
Something like arguments.
J.
S. And then we're going to go ahead and mess around with three examples.
The first two exploring the features and the last one.
A challenge for you.
We're going to kick things off by creating a function that takes multiple arguments.
It is going to take in multiple numbers add them all together and return the sum.
So if we were going to set that up we could use that to create add and set it equal to right here.
What are we going to set that equal to a function and that function is going to take multiple arguments
but for the moment we'll just put nothing inside of parentheses.
Now if we were to call add and do something with that result it might look a bit like this.
Let result equal a call to add and then we can do something with the result like printed cancel that
log result.
Now this is just a little template for how things might look but it doesn't actually return anything.
So result is going to print undefined which is what we learned about in the last video.
Now let's actually set up the arguments and function body for ADD.
If we wanted a single argument for AD What would we do.
We would just name it right here.
If I was going to call it a for example I would set up a.
And then I could use a down below.
Assuming it was actually passed in.
So for example I could pass in the value 10.
Now if I want to pass in multiple arguments how do I set up the definition right here.
All I do is I separate their names by a comma so comma and people usually add a space for formatting
purposes and then right here I can pick what I want to call the second argument that's provided.
So for example I could call it B and then I could pass in a second value by separating it by a comma.
So right here 10 for the first value and let's use one for the second.
So with this call signature we're going to have a equal 10 and be equal one.
So if I were to add them up and return the value I would expect 11 to come back.
Now we can go ahead and create a variable where we add the two numbers together and then return that
variable.
But you can actually just provide the expression right in line.
So I can return a plus b.
This is going to add a and b together resulting in 11.
It's then going to return it.
And if we run our script we should actually see some printing.
Let's go ahead and save the file and check out what happens over here in the terminal.
I'm going to run it node arguments Dhananjay s when I do I see 11 printing which is exactly what we
were hoping for.
So if we want to provide multiple arguments we just create a comma separated list both in the function
definition and in the call signature where we actually pass values in to the function.
Now we can go further.
There's no reason to stop at two.
You can have as many arguments as your function needs.
So let's say we needed a third one comma with a C afterwards and then we could provide a third value.
For example I could provide five.
Now I would expect the sum to be 16.
Assuming I actually use C right here.
So a plus b plus C..
The result should be 16 and if I rerun the script using the up arrow and enter and that is exactly what
we get.
Now there's really not a whole lot to it.
You can add as many arguments as you want.
You can have a function with no arguments and there's no rule for when to use which it's really going
to depend on what the function needs.
So if we're creating a function that calculates your income tax we're going to need certain values we
might need one argument for example the amount of income you actually made other functions will be more
arguments other functions will need less.
So we've explored how we can set up multiple arguments.
Now what I want to do is figure out how we can provide a default value if an argument isn't actually
provided.
When the function is called Let's go ahead and explore this with a second example down below right here.
Default arguments add up above at the very top of the file.
I'm just going to leave a little comment about this example here.
We explored multiple arguments.
Awesome.
So how do we use default arguments.
Let's explore with an example.
Let's imagine we're creating a multiplayer video game and there's a leaderboard right.
So all of the players ranked from the people with the most points to the least.
And there's two pieces of information that we're going to need in order to generate the individual row.
We're going to need the users name and their score.
Now not everyone is going to have a score that you haven't played a game yet.
We're going to come up with a good default value will use 0 and not everyone is going to provide a name.
Maybe you want to remain anonymous.
So we're going to come up with a default name as well.
Let's explore what this might look like.
We're still going to be making a function.
I'm going to call this one.
Get score taxed.
So this would just be one part of our video game and I'm going to set it equal to a function and this
function is going to take two arguments.
The name of the player and the player's score.
Then down below in the function body we're going to go ahead and actually use these two values to generate
a string before we do though.
Let's just use console dot log to print out name that on the next line.
Cancel that line.
To print out score.
And what we're going to do is call the function down below.
I'm going to call get score taxed and I'm going to provide no values.
I'm going to call it just like that's based off of what we learned in the last video.
I know that name and score will both be undefined and if I rerun the file I would expect to see undefined
twice.
I'm going to rerun things over inside of the terminal.
And what do I get.
I get 16 from the first example and undefined twice from the second example.
So by default if we do not provide an argument value nothing is going to show up but we can actually
set up defaults and we do that right here in our function definition.
So when I define that the second argument should be called score.
I can also provide a default value for that.
So if it isn't provided use this in this case we can set it equal to the number zero.
How do we do that.
Well after the argument name we use space equals space and we give it a value.
So the value could be a string or a boolean.
Anything in Javascript in our case it makes sense to use a number since we're talking about a score.
So I'm going to zero right in here.
Now we could say that score has a default value.
If we change nothing about our function call and rerun the program we're no longer going to see undefine
actually going to see zero.
So what happens here.
The function executes.
It says all right call that second one score.
There is no value so use the default.
And that is what prints.
Now should we provide a value in the call.
It would be used.
So for example if I set the name equal to Andrew as the first argument and the score equal to 100 100
is going to get used instead of zero.
This value only gets used if there is no provided argument value.
So in this case I would expect to see Andrew and 100 and that's exactly what I get.
We overrode the default value because we actually provided one.
Now we could do the same thing for the first argument as well.
I could set a default value for this so after the argument name space equals space.
And in this case we're going to provide a string value as the default value.
So we can open and close some strings using single quotes and right inside I can provide that value
and I'm going to call players with no name.
Anonymous excellent.
Now we have default values for both.
And this makes the function a little more robust.
We could provide no values we could provide all of them or we could provide some combination in between.
So let's go ahead and delete all of the values we pass into the function and just make sure our other
default is working right here.
We run things.
I get anonymous and I get zero with the default in place.
We can actually work on making our function do something meaningful with these two pieces of information.
So in the end of the day get score.
Text is going to return the score text and I'm going to assign it to a variable.
Let score text equal whatever comes back from calling get scored text then I'll print the con..
Lague score.
Text.
Now all we need to do is to actually return a string from this function as opposed to just seeing what
the argument values are.
So I'm going to delete those canceled out log calls.
Right here we can return a new string.
So for this one this is going to be the individual line in our leader board.
I'll start off with the name calling space and then I'm going to concatenate on the name whether it's
the default value Anonymous or a value actually provided when the function was called.
Right here though either way we get access to that via name.
So that's the first part.
And the second part I'm going to concatenate on a space then score call in space and then their score.
So right here plus score.
Now we're going to get a useful string back.
And when we call the function and print it we should see our little string with the values provided
in this case default values for both since none are passed in to the function call.
I'm going to rerun things from the terminal and from here we get just that name Anonymous score zero.
We could actually put a another little hyphen maybe right in between to make things a little easier
to read.
Awesome.
Now let's go ahead and provide values.
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
score out of the function call.
That means the default value is going to be used.
And if I save the file and rerun it I get name Andrew and I get score 0.
We could also do the opposite.
I could say there is no name but they do have a score.
Now if I want to provide the second argument but I don't want to provide the first what I have to do
is use undefined right here.
So undefined comma followed by that second value.
Let's go ahead and say I have a score of ninety nine.
Now I would get name Anonymous and score ninety nine so he can provide no value for other arguments.
We just use undefined right there.
And I remember the only reason I'm doing this is because I want to get access to the second argument
and actually assign a value.
So I do have to provide something for the first one.
If I left things like this looks like this what would happen.
I would have 99 for the name by providing undefined.
I make sure that ninety nine get set as the score.
Perfect.
Now it's Challenge time down below.
Let's create a little challenge area and what you're going to do is create a new function that has multiple
arguments and uses the default arguments.
We just explored this little function is going to be a tip calculator.
So you could call the function something like get tip that's an action that we go to a restaurant and
we spend 100 bucks and we want to tip 20 percent.
What is that number that would be a 20 dollar tip.
We're going to create a little function that figures that out for you.
So right here the function is going to take two arguments.
It is going to take the total for the bill and it's going to take the percentage you want to tip.
You could call this like tip percent for example.
Now for the total It doesn't really make sense to have a default.
Each bill is going to be different depending on what you ordered but for the tip.
We could go ahead and come up with a default tip percentage.
We could say 20 percent for example and we're going to reflect this as a decimal point.
So point five would be a 50 percent tip.
Point one would be a 10 percent tip or point to for a 20 percent tip.
We take the total 100 we multiply it by point two that is going to result in 20 which is the correct
tip value.
All you're going to do is run that calculation return the value that's going to be it for the function.
So two arguments a default for one of them then I wanted to go ahead and actually use it.
Call the function a couple of times with different values for the total and the tip.
Make sure that the default 10 percent shows up and make sure that if you provide a different one like
point three that actually gets used.
Take as much time as you need to create this function and mess around with it.
If you get stuck trying to work through your problem and then we're going to come back and go through
things together paused the video and when you're done click play
all right how that one go I'm going to kick things off by creating my function and I'm going to do that
right here.
Let me get tip equal a function.
This function is going to have two arguments.
We'll provide those in just a second right here.
The first one is going to be the total for the bill.
Once again no default for this one.
Each one is going to be different.
The second one is going to be the tip percent and that's OK if you call these things something else.
That's not the important part the important part is the actual function setup itself.
Now for the tip percent we are going to come up with a default value.
I'm going to set it equal to point two.
So by default you can just provide the total and we will calculate the tip based off of that 20 percent
number inside of it.
What do we need to do.
We just need to multiply the two values together and return the result so I can return total.
Times tip per cent perfect.
Now down below we can actually use that function.
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
to pass in a total let's say 100.
Using that example I came up with before and for this one I'm not going to provide a tip percent value.
So it should use the default point to right here cancel dialog tip.
I'm going to remove the comment and then I'm going to actually run the file.
Let's save things and execute them.
I'm going to use clear to clear the terminal output and the up arrow key twice to rerun the arguments
file right here what do I get for the tip I get 20 which is indeed correct.
Now let's override that and provide a different type percent.
I'm going to provide pointer to five.
A 25 percent test.
And now if I rerun things what do I get right here I get 25 bucks and if I were to change the input
value selling it to something like 40 I would get 25 percent of 40 which would be 10 bucks and if I
rerun it that's exactly what we get.
So real world functions aren't just going to have a single argument like we saw in the past real world
functions are going to take multiple arguments.
There are certain values like total that we're going to require.
There are others like 10 percent where it does make sense to have a default value and are going to get
a lot of experience with this as we actually use functions in the projects we create.
Now that we know this though we are ready to continue learning about functions and we're going to do
just that in the next video.
Stay tuned.
I'll see you then.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.