Recursionexaplanation of Recursion Very Much Important
Recursionexaplanation of Recursion Very Much Important
including an example.
Here we will try to explain recursion as simply as possible, without getting into the extremely
theoretical and mathematical aspect of it. We want to provide a practical explanation that
would be easy to understand so think of this as an easy to understand tutorial on recursion (for
beginners or dummies). Defining recursion is easy any routine that calls itself is a
recursive routine. You may be more familiar with the term method or function, but we use
routine since that encompasses both. Although the definition of recursion is simple, using and
understanding recursion is difficult so you will need to read this article with patience and you
will (hopefully) understand it.
x! = x * (x - 1)!
And, something else that is important to know is the fact that the factorial of 0 is equal to 1 as is
the factorial of 1.
0! = 1! = 1
Where x is the number whose factorial we want to find. And that is what recursion is all about
finding repetitive patterns, and breaking a problem down into repetitive sub-tasks.
Think about this what do you think would be a good base case for this problem when does it
make sense to stop the recursion? Well, it turns out that the base case would occur when the
factorial function hits a value of 1 because at that point we know the factorial of 1 is 1, so we
should stop right there. And, it doesnt make sense to allow the function to find the factorial of
numbers less than 1, since the factorial is defined for integers between x and 1.
So, heres what the Java code for our recursive factorial method would look like:
}
Call Stacks, Recursion, and Stack Frames
A call stack is a data structure used by the program to store information about the active
subroutines (like functions in C++ or methods in Java) in a program. The main reason for
having a call stack is so that the program can keep track of where a subroutine should return
control to once it finishes executing. For example, suppose we have a method CreateBox
which calls another method CreateLine in 4 different places. If the program has finished
executing the method CreateLine, then it needs to know where in the CreateBox method it needs
to return to. This is why the program uses a call stack so that it can keep track of these details.
You can see that the first stack frame is created with x equal to 3. And then a call to Factorial(2)
is made so the first call to Factorial(3) does not run to completion because another call
(Factorial(2)) is made before the very first call to Factorial can run to completion. A stack frame
is used to hold the state of the first call to Factorial it will store the local function
variables (and their values) of the current invocation of Factorial, and it will also store the
return address of the method that called it (since we are talking about the very first non-
recursive invocation of Factorial, whatever routine invoked Factorial in the first place is where
Factorial would return when it is completely done with everything) . Because the stack frame
also stores the return address, the Factorial function knows where to return to when it finishes
running.
Finally, in the 3rd stack frame, we run into our base case, which means the recursive calls are
finished and then control is returned to the 2nd stack frame, where Factorial(1) * 2 is calculated
to be 2, and then control is returned to the very first stack frame. Finally, our result of 6 is
returned.
Conclusion
Hopefully, this article helped you understand recursion better. Now, if you would like to read
some more interview questions that have to do with recursion then continue on in this section, or
you can read one of our personal favorites right here: Recursion Interview Question.
long factorial(int);
int main()
{
int n;
long f;
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Recursion is a technique in which a function calls itself, for example in above code factorial
function is calling itself. To solve a problem using recursion you must first express its solution in
recursive form.
#include<stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}
Output
#include<stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}