Welcome back.
Today, we going to talk about JavaScript functions, how we can declare war on, how we can call one,
how we can execute a function in what function expression means.
OK, let's start working.
So create a lecture five file inside, put a node call and call it JavaScript function.
Now, also here, make sure that you linking that file to external file.
That's why we need to link.
Now we have used two functions so far.
The first one was alert in the second one was prompt.
Actually, this was the first one.
This was the second one.
But that doesn't matter.
So if we go to a browser and we type prompt and if you hit enter just like this, don't put the brackets,
it will say it's a function if you put alert.
Oops, Leard.
It's F stands for function, but what are these brackets, let's use them.
Wow, now you see that we are calling and executing the function, right?
That is pretty much it.
So these oops, sorry, these let me just show you this.
These brackets means call and execute the function.
And we usually do them like this.
That means end of an expression, because when we are one to end expression as explained, if you don't
know, just watch the previous lecture or two lectures, I'm not sure, but just explain what the expression
means and why we need to end it.
OK, but we are not going to use this inbuilt functions.
We're going to create our own.
So let's create our first own function in.
Let's declare it.
Clear a function, so F stands for function.
It's a shortcut, so we need to use the key word function.
That's the Goldway my first.
Function and this is my name, I can name it the functions that I want, you can put here my 100 my
my this is my thousand function.
Whatever you want to be here, my name, whatever you makes you happy.
But I just put it here because it makes sense.
Now, in order to know that it's a function, we need to use this parentheses and this curly brackets
means a block of code will go here.
Now console the lock I'm going to use here and I will say so you're going to say hello from here.
Save it now if you go back.
I need to use this.
If you go back, refresh here, you're not going to see the result in.
Why is this?
Well, because we need to call it function.
So we just declared a function.
But we now need to call the function by calling the function with these brackets.
It will actually mean call the function and execute.
Now go back.
There you go.
That is pretty much it.
Now, what is more interesting, a function can have one or more arguments.
Now you will see that this is mixed up with parameters.
Uh, spelled it right, right.
So arguments and parameters, but.
You don't have to worry about if you mixed these as well, because in old tutorials or books, you will
see that they used arguments and parameters.
But I want you to know the read the reality so functions can take arguments in what are the arguments?
Well, we already use them.
These were the arguments for the alert alert rackets and high.
So it would it take one argument that this string this is this is and maybe I didn't tell you back then,
but because we haven't done the functions now there is the time.
So I'm going to create a second function called.
My second, actually, let's put it like this function argument function, and you can put any name
meaningful that you want in here, we will see first argument.
Very good.
Now, you can give any name of the argument and let's say control that log.
The argument only I'm just.
First argument, whatever we passing to the function, that's how you call it, you passing an argument
to a function you're going to control.
So I'm going to just print it out.
So what are we going to do now?
Argument function.
Very good.
And now we need to pass that one so it can be screened.
It can be boolean, it can be any data that you JavaScript type that we have encountered so far.
But I'm going to say hello.
I'm just going to pass a screen and I'm going to save it.
In there you go.
This is the helo that I passed to the function as an argument.
So even if I changed this to just a but I need to call it here as well, A, that it will still work.
This is irrelevant.
What we put here, every name we put here, it's irrelevant.
But what is relevant, what we are passing to function.
Now, this is one argument function, whatever.
What if we have more arguments?
Let's create a function that will take multiple arguments.
For example, let's create a function that will calculate the year that we are born.
So function.
Bert.
Birth year and coup, you can name it whatever you want, first parameter will be your age and now inside
I will see your birth year
again.
I'm naming variable as I want.
You can just change the name.
Now, I just want to say a year minus age.
So whatever we passing to the function here, when we call it, we're.
Here and here, we can pass directly 20, 20 to say 23, cool.
Now the Bertier will get the result of 20, 20 to minus at the age.
Nice.
So console at last.
The birth year.
Save it.
Very good.
We do have the result.
One more thing that I want to show you is that, oh, actually, I didn't see this.
When we have multiple arguments, we need to space them with comma.
Same as here.
Very good.
Now, what is function expression?
And this is very important for you to understand.
Well, let's create a variable called function expression, and that will be equal to a function.
Oops.
And comes along.
Function.
Here.
Now, this is coming from function expression, very good.
Now, what is happening here?
Well, we declare a variable.
And this equal sign, it's not the same as Inmet, this equal sign means these equal means assign a
value to something, right?
So whatever this function returns, we will assign it as a value of this function.
But now dysfunction doesn't have a name.
It's called anonymous function.
Now, the function that they don't have any name, it's called anonymous function.
And this is the beauty of function expression.
Now, there are examples where you can see here we can declare the name, but I'm not going to do that
because those examples are very rare and you don't need them.
OK, so how are we going to call a function without name?
Well, we can use the variable name.
How cool is this?
I'm in the mood today, and that's it.
We don't have any arguments, but we can have one or two or many arguments here as well.
Same as the other ones.
Let's see.
Save it, boom.
There you go.
The cons a lot.
Nice.
So one more thing that I want to show you two more things that I want to share with you tonight and
is that that we are at the moment consoling the output.
And if we remove this one, this nineteen ninety nine, it will be gone, it's nothing, dear.
And why is this?
While console lock is just for us, we printing the values here to be able to see it so the user can
see it.
But the result of this function is stored nowhere.
Right.
So what's the solution?
OK, I'm going to copy this one scan uncommon.
This one.
Return, I'm going to use return nouse, so Bourdier, one word here, one in here, instead of calling
lot, I'm just going to say return the bird here.
So what do you think it will happen now?
When I call this function here with these two arguments, it will go inside, it will do its job, do
it calculation and it will return the result of this, whatever it's taught here.
But how I'm going to access it outside, because if I do it right now, there's no another nineteen
ninety nine and I want so these Bertier actually now can be canceled.
Look again.
That's the one way.
It's.
My hands are not working tonight, so there you go, but this is no different than it was here, right?
We can be better than this.
Of course.
So I'm just going to comment on this one.
But this bad boy here.
So I'm going to see more result or I'm just going to say birth year needs more meaningful like that.
And I'm just going to put here the function.
I'm going to call the function with the two parameters.
Let's make it 20, 23.
So that will return a different year and now we need to cancel a log this birth year because the result.
Oh, now I do have a problem.
See, I have used bird ear and bird ear as a function.
Now, we can't do that.
So bird ear result.
We can't call a function and the variable.
Same name.
Two thousand nights, it's our function is working, so in this budget year result, we have stored
the value of dysfunction and we can use it now.
For example, if I want to subtract one year minus one year, make a breathing space, there it is so
we can reuse it.
That's called code reusability.
Now, return is a key word and it's.
We can't use it as a variable, so even we have this different color, it's a purple for the function
and for the variables, we also have a little bit different color and for the functions name.
Nice.
So this is predefine key keyword that we're not going to be able to use.
It's taken.
Right.
Nice.
So let's do one more thing.
And this is a final.
I promise.
So let's create a function that will take four parameters.
Nice.
Now we are talking, but we will use this Bergère function inside the function.
So that's mean.
We can call the function from a function so we don't need this.
So function name the new function.
Name will be print details.
Nice.
This is good function name.
So it will take first name as an argument, last name and year and age.
Now this year and age we will use them and we will supply to this function.
But the first two that's actually been these days, it's so consulate lock we will use for these print
details.
So my first name is we're going to read Spazz there plus first name and what I haven't shown you what
we can do a new line just like this.
Now, compare this to more times, my last name is Make sure you change here, the last name, this
variable, whatever we pass as an argument.
And now.
I am born oops in and we can say here we can call the function birth year that's called the function
birth year one and pass year and age nice.
Save it.
So what what else we need to do is just to call print details and now you see how these two functions
will work.
So this function will start doing these line, then the second line and it will stop here and it will
do the calculation of this.
OK, so we need to just pass in some details with your details if you want.
So don't put my right.
It's not good.
Make sure that you put in your own details.
And we are just putting twenty, twenty two here and thirty three and save it.
And let's see.
There you go.
This is the result.
So now you know how to call function from inside a function.
So I will see you in the next one.
And we are done for tonight.