Welcome to my YouTube channel.
Today I will be discussing on how to find
the Factorial of a number in python using
recursion.
What you are seeing on your screen right now
is just a fun way of representing what recursion
is.
Recursion is Not that difficult to understand
as I will explain this to you in a few moments.
• So we have two problems in our hand.
First is of Factorial, and the second is of
Recursion, ie we have to solve this using
recursion.
• Factorial of a number, as its definition
is the product that we get when we multiply
this number by all the preceding natural numbers.
that is Factorial of 5 will be 5*4*3*2*1 = 120
• So as the definition holds, we can only
find the factorial of non negative integers
that is factorials of 0, 1, 2, 3 and so on
only exist.
At this point I need to mention that Factorial
of 0 is 1 and this is an accepted convention.
• The second problem that we have is that
of recursion.
So what is recursion.
Simply put the property of a function when
it calls itself is called recursion.
• You may have gotten dizzy at this moment.
Don’t worry!
Allow me to explain this in a more intuitive
way.
• When we say a function is recursive in
nature, it means that, that function is calling
itself or invoking itself.
So now the invoked function by the very nature
of recursion will again invoke itself right?
So this will go on forever UNTIL….. we provide
it with a termination condition.
In a recursion function, such a termination
condition is called a “base case”.
So whenever a recursion function calls or
invokes itself it must get one step closer
towards its base case where finally this recursion
will stop.
So whenever you use recursion, you must and
I cant stress this enough, you must always
provide a correct base case.
You must check it and provide it correctly.
• That’s all the information you need
to know about recursion.
I know it might be mind boggling and mind
numbing thing at first, but trust me you have
to take a leap of faith to properly understand
recursion.
Now I will explain recursion with respect
to the factorial problem we have at our hand
right now.
• My advice would be not to see this problem
as a “factorial problem using recursion”.
But see it as “understanding recursion using
factorial problem”.
Because we all know what factorial of a number
is.
• So lets jump right to it, shall we?
• We know that factorial of lets say 4 is
4*3*2*1 = 24 right!
This means that factorial of 4 could also
written as 4 * 3!
Right?
Because 3! = 3*2*1.
That’s why 4! = 4 * 3!.
• Again this 3!
Could be expressed as 3 * 2!
Right?
Because of the same reason
• And 2!
Could be expressed as 2 * 1!
• And in the end, we know that 1! = 1.
• Then we will return the value of 2*1!
Which is 2*1 = 2
• Then we will return the value of 3 * 2!
Which is 3*2 = 6
• Then we will return the value of 4*3!
Which is 4*6 = 24
• So the final output that has been returned
is 24.
• So the thing to keep in mind in recursion
is, we unfold the tree by invoking the recursive
function again and again until we reach the
base case, and then we return the value of
whatever operation we did, in this case it
was multiplication, till we return the final
output.
Now the code will look something like this:
We will define a function with the name Factorial
which would take an integer as its argument.
Note that this integer has to be 0 or greater
than that.
if the argument, here in this case which is
n is 0, then we will return 1, because factorial
of 0 is 1.
Now we will define our base case, which is
if n == 1, then return 1, else in every other
case return n multiplied by the Factorial
of the preceding number which is n-1.
This will go on, until we reach Factorial
of 1, in which case it will return 1.
That’s it!
Youre done now!
It will look something like this in the Spyder
IDE.
So just 
to recap what just happened here, recursion
is just the property of a function when it
calls itself again and again until it is made
to stop using a base case, or a termination
condition.
So if you have understood it, and if you have
liked the video, I would encourage you to
subscribe to this youtube channel for more
such exciting contents.
Till next, cheerio
