So in this video we're going to pick up where we left off with Javascript functions and the next thing
we're going to discuss are arguments.
So we've seen how to write a function like sing song which does the same thing every single time every
time we write sing song.
As you can see here it just calls these four lines of consul Dalt logs.
But what if I wanted to have a function sing song that could sing a different song.
So maybe it could sing Twinkle twinkle little star but it could also seeing three blind mice.
And Mary Had A Little Lamb and I wanted to be able to call sing song and tell it which song I wanted
it to sing or here's another example.
If I had a function called Say hello.
And it just cancelled out log hello if I wanted that function to be able to say hello to a specific
person I wanted to personalize it so that I could call Say hello with Jeffrey and he would say hello
to Jeffery I could call Say hello with Sally and he would call Say hello with Sally.
So to do that we need to talk about arguments.
So arguments are how we can write functions that take inputs so far or functions haven't taken any inputs
.
But here's an example of a function called Square and what it does is it takes a number any number and
then it counts adult logs.
The square of that number.
So the syntax to say that a function is expecting something to be passed in that it's expecting an argument
looks like this rather than just empty parentheses here.
We put the name of an argument.
So in this case we called it numb and this can be called anything at all.
It's just a placeholder.
So whenever the user calls Square and we pass in ten numb is going to hold the value of 10 temporarily
if the user calls square with three Nahm is going to hold the value of three.
And then we can use Gnome inside of the function.
So in this case cancel dot log numb times numb.
It's just going to take whatever number was passed in the parentheses and square it.
So as you can see here's the examples.
Square 10 prints 100 square 3 print 9 square for prints 16.
So let's go ahead and write our own function to illustrate how we use arguments.
So we're going to write one called Say hello.
So function say hello and to start.
It's not going to take any arguments it will just look like this.
And then inside of here I'm going to type cancel that log.
Hello there with exclamation point.
And that's all your function will do for now.
And let's make this a little bit wider.
I hit enter and now I can run say hello and that's all it does.
So next I want to define a function called Say hello.
That takes a person's name and then it says hello there plus the person's name.
So to do that I'm just going to redefine say hello and this time it's not empty parentheses I'm going
to put something in here we can call it name or it person or a user.
I'm going to go with name though.
And now I'm going to write console dot log.
Hello there and then I'm going to add in name just like this.
And then I'll add my exclamation point at the end and then close the parentheses.
So as you can see name is just a placeholder for whatever value we pass in.
And then we're going to cancel that log.
Hello there with the name in the middle.
And then an exclamation point.
So to call this we now say say hello and then a name like rusty and we get hello there Rusty.
And this time I'll do say hello Charlie or say hello Mary and you can see that our code changes our
function has adapted to whatever value we passed in.
So the use of arguments is what makes our functions super powerful.
So on a Web site like Facebook there might be a function called Make home page and that make home page
is going to take information as arguments about the user who's logged in.
It's about making a little machine that can take in an argument and then spits something else out.
Also we're not limited just to one argument on this slide you can see that our functions can take multiple
arguments.
So here's an example of a function that calculates the area of a rectangle.
It takes a length and the width and then we just multiply them together.
Count about log length times with and then to call this function we just pass in two numbers separated
by a comma.
So the first value corresponds with length.
The second one corresponds with with.
So it just comes down to the order.
So if I switch these two and I put two first two would be length and nine would be with.
So here's another example where we have three arguments person one person to person three and then we
just say hi to all three of them with separate console that logs.
So let's copy this one and I'll just demonstrated here so I'll paste it in.
Now if I call greets with three names.
Harry Ron and Hermione the order dictates who is person one Harry who's person to run and who's person
three her mind.
And you can see that those values are placeholders and we get.
Hi harry.
Hi Ron.
Hi how my any.
One other interesting note is that I can write a function that is expecting an argument like right.
But if I leave those arguments off when I call it.
So if I just do this Harry and Ron and I leave her my auntie off I would never do that.
By the way as for mine he's my favorite but if I did that you see I get.
Hi harry.
Hi Ron.
And then a high undefined.
So it doesn't break my code.
I don't get an error message.
But what happens is that person 3 is just left as undefined in some programming languages.
This would stop everything and throw an error message in javascript.
It's totally fine.
It just means that you might have to watch out and check if something is undefined or not.
So arguments are one of the important pieces and functions that make them really really useful because
it's not only about shortening our code and repeating the same chunk of code every time like Twinkle
Twinkle Little Star.
It's also about making that code change a little bit depending on some inputs.
So here are a few examples of when we might use arguments in a real web app.
So imagine that we have a web game and in that game there's a score for every player and the player
can do things that can increment that score of suit or decrement that score.
So if a player does something crazy we might have a function called add to score and if we want to give
them a lot of points we would pass in 100 points.
If a user died we might subtract 100 points and if we want to award just a few points you might do something
like I had to score five.
So here's another example.
Imagine we had a site that had user lock in.
So we might have a function called check credentials and it would take two arguments an e-mail and a
password.
So we might pass something in like Rustie at gmail dot com.
And then a password like with 1 to 3 and then check credentials would take that e-mail and the password
and it would check them and make some decision.
So there would be probably an if statement in there.
It would check if they match correctly and if they did then log Jasin else it shows us an error message
.
So those are just two simple examples but we'll be writing code like that later on in this class.
The ability of functions to take arguments is one of if not the most important part of writing functions
.
It's not just about repeating code.
Singing the same twinkle twinkle little star over and over and over.
It's about repeating code that we can also change a little bit that we can have some variables in.
So arguments empower us they let us do that.