All right, let's finish up the concept of function by talking about what is called the return keyword.
All right.
So before we talk about the return keyword, let's solve this exercise.
So let me comment out this one here.
I don't see many console.log.
Or I can I can comment on this one.
Okay.
So the function goes like this one we can use.
Let me change to use.
I mean, function declaration.
So function function.
And then I say multiply, multiply numbers.
Now my square bracket and my function body here and here, it takes three things, right?
I can say A, B and C in a name here.
Anybody here can see number one.
Number two and I'm three.
It's up to you.
So I will declare variable counts or let end of this one go.
So a result is equal to A, times B, times C.
Now when I call the function multiply num numbers are pass in three things, right?
Pass in them passing one, two and three meaning.
And let me console.log the results here.
I didn't do it.
I'm going to look at the results here.
Okay.
So here A is one, B is two and C is three.
If I console.log it to be one times two, some three, what would be the answer?
One times two is what is two times three six?
Let's try.
Let's save it.
Indeed, we have six.
That is the solution to this question.
Okay.
So last part you talk about is what you call the return keyword.
So let's use this one or let's use this one to explain more.
Okay, let me copy this one here.
Let me comment out and let me comment out this one also and let me paste this function here as before.
Okay.
So let me come in here and call this one the return.
Keyword return after this one, then you'll be a guru when it comes to function creating.
Okay.
So what is the purpose of the return key?
We need a function.
So at this time, right now.
Well, the purpose or the only way this function is doing is that it's taking in some dynamic value.
Make it more easy to read.
Let me see.
Three +47.
We can all.
Really.
Okay.
Let me save it.
Yeah.
So what this function is doing is that, well, it has sort of problems about I mean, we have to centralize
our code in one place, but the governance here is that this function in time, I call this function,
I can call it many times, as many times as I want.
Let's see.
We see we have seven, seven here.
But here comes the problem.
Let's say that I want to display the result of my function into the page here.
But don't worry.
We haven't talked about Dom manipulation, so spare me with that.
I'm going to give you some some scenarios why this function is not performing the way we want it.
That's the scenario, number one.
And also, what about say that?
Oh, no, I don't want this play my result into the page, but instead I want to display it inside an
elect.
How can we do that?
In that way, we need what is called the return keyword, because this function is not giving out anything.
It just console.log in it.
So there were ten.
Key is used to exit a function and return a value to the caller.
Here comes.
So one point of return is it is used to exit a function.
I would I would explain to you with code right now.
Okay.
And then if a value is present in the return, it is going to give out to the variable we assign a function
to.
The return keyword is essential for creating functions that return value.
So this function is returning values.
So without it, function will only be able to perform actions but not return values to the caller.
This would limit the usefulness of a functions and make them much less powerful.
So that's what we talk about right now, right?
Because this function, even though it's doing some action, but it's not doing it's not more useful.
That's why we tend to come in here.
So here it comes.
Now, instead of returning a console of this one here.
Now you bring in the return keyword from JavaScript.
And then what are we giving out or return from?
This function is the result.
So many that JavaScript runs our code line by line.
If it gets here, first of all, declare a variable.
And here you go to a line number eight where I have a variable called result and give me X plus Y,
which is three plus seven.
Let me remove this one from here and let me save it.
Now, see that we have nothing here, even though we have the risk, but because we have removed the
console.log and then we are using the return keyword, how are we going to get the result here?
Here comes.
So here we have given the result to return.
It means that when you call it a function, we have the value inside the return keyword with the function.
So to anytime, use a function, use a return keyword.
Again, when you call it, you assign the result to a variable.
You can see that led.
Now we can say and so is equal to now because of the return keyword.
This one represents the result coming out from this function.
So now behind the scene, this is seven, right?
And it's now in the variable of and so I can take this variable and use it wherever I want.
I can console.log it.
I can display it here and wherever I want display I can use it.
So if I console.log the answer.
Check it out.
Now, you see, I have seven.
I have seven.
That is 40.
Or if I don't want to call it here, I can use a method called select.
I let that display something in the browser.
Okay, so here I select and then my answer.
If I save it.
Now, you see that we have taken the result and display it here.
Now our function is more reusable because now we can take this function and then display the result
any way I want.
Let's talk about the exit, anywhere or inside, any function.
If it sees return keyword it exit meaning is top the the function or the logic inside of a function.
So now to prove to you let's console.log something here console dot log let's say function code like
this one.
So if I save it, have a look.
Let me remove this console.
Let me.
Alexia.
If I press the piece, I see what function call, because when I call this function, it is reading
line by line.
It gets here x plus x, which is five.
Give it to this one.
Go to console.log.
That's why it has to be displayed here, because we have called a function here.
So but let me say function.
Let me say before before return keyword.
Before retain.
And then the same thing here.
We're down here and say after ten.
So when I save this one, we see that.
Let me save this function.
You can see that this one has called the first one, meaning the function before return has caught.
But this one.
You see that this one has been degraded, meaning this punch will never be called.
As soon as it gets to this return, it exits.
The function will not continue.
That's why we couldn't call this one.
But if I remove the return from here and save it, we see the two of them has been called.
That's the purpose of a function, the return keyword.