Hi, guys.
What's up?
So today we are going to solve one more interesting problem on functions.
So the Brownless statement is a function Laura done.
Factorial of a number.
Thought he'd done factorial over.
No.
So what is Vectorial?
So five factorial.
It was five to four and two, three in total.
And doing what you can, right?
One and two and two, three and two forward.
And two.
Five, which is 120.
So what is one factorial?
And there's one more.
There's two factorial.
It is one and two.
Two or three factorial.
One and two doing two, three, six.
What is four factorial?
One and two.
Doing two.
Three and two.
Four.
Which is twenty four.
OK.
So given a number and given a number, then we have to write a function that will on the factorial of
that number.
So how we can find factory number, for example, numbers.
And so what do we do?
One and two, two and three into four.
And.
OK, so I just we have to multiply all this numbers.
So I need a loop.
Initially, the loop will start from one Lowville go, then end.
And what I will do, I will take a variable answer.
Answer will initially be one.
So answer quiz.
Answer.
Multiply by I.
OK.
So let's see.
Function Factorial Nard CBB.
So what I have to do, first of all, I will take a number and as input from the user who's Vectorial
and we have to conclude.
So we need a factorial function.
Let's say the name of defect, all function is fact.
It will take and as input was the toll, it has to return.
So since it will be done something, I will have to store it.
So let's say in Tancer, it was fact.
Now, after getting the answer, I will print my answer.
So that is all that we have to do.
Now let us write the function fact.
So this function will return an integer.
That is the fact all of the number and the name of the function is fact.
It will take a number as input was factory.
It will calculate.
So.
Let's say I am having a variable and dasa, which is initialized to one.
OK.
And now we have to multiply starting from one.
So I equals when I last then ordered close to an A plus plus answer equals answer my two blabby I.
And after calculating the answer we have to read Dundy answer.
OK, so this is all that we have to do.
So now let us test our program.
Led to the relief and is for so 24 is difficult enough for now, let's see, where does the factor of
five?
So the factor of five is one 20.
So our program is working fine.
And why I initially started with when I feel a lot initialise the answer will contain a garbage value.
So we have to initialize, just like we used to initialize some equals zero.
So we have to initialize it.
So you can also start from two, because multiplication with one doesn't have an impact.
So five victory leaves 120.
One more thing that I want to tell you is so you people should not get confused, this variable answer
and this variable answer.
They both are different.
Why?
They are different because their scope are different.
The scope of this variable answer is between this and this.
And the scope of this answer is this.
And this.
OK, so here answer is a local variable.
This is also a local variable and these scopes are different.
OK, so these variables are different.
Don't get confused.
They don't have any relation.
Also, this variable in and this variable and they're also different.
The scope of this variable is between this bracket and this bracket, the scope of this variable and
as between this record and this beckert.
So I said and n they both are different.
OK.
They both are different.
Why?
Because their scores are different.
So do not get confused at all.
So let me show you what I want to say.
So for example, I am changing the value of and let's say it was two.
OK.
And for example, I'm here I am bringing the value of an S.O.S. out.
I'm bringing the videophone.
And let's come into doubt.
OK, so let's give fifers input.
So I'm giving fifers input and I am printing the value of.
So.
Value and will not change.
So five is getting printed.
OK.
So what I did is.
So what I did I give five input.
I call this function and this function change the value of N two two.
But when I am printing the answer is still five.
The value of end is still five.
Why.
Because this n and this n they are different.
OK.
They don't have any relationship and they're completely different.
OK.
So how are trends.
This is my call stack.
And this is my function.
Main function mean.
This is calling.
It will call function in fact when in fact function will complete.
When in fact function well you're done.
It will be popped out.
I will start executing my mean from line number 24 because this is coming command and then.
When the work of mine gets completed, I will pop it out.
And finally my Kostic will become empty and my programmers finish.
Oh.
OK, so remember this answer variable.
This answer variable, completely different, this and this, and they are completely different.
OK.
So instead of storing you can directly, you can also print dramatically.
You can also see out fact often and let's be clear, did.
OK, so how does working.
OK, guys.
So this is it for this video.
If you have any doubt, feel free to ask.
Thank you.