Recursion is the way of programming in which a function calls itself. If a function definition satisfies all the conditions of a recursion, such a function is called a recursive function.
A recursive function has to be handled very carefully. If a recursive function fails to terminate then it may lead to an infinite loop.
A recursive function terminates, when it reaches a base case. A base case is a simple situation, where the problem can be solved without using any further recursion. If the base case we provided is wrong, our problem may end in an infinite loop.
Now let’s check one of the basic examples of a recursive function. Given below is the implementation of factorial using a recursive function in python.
def factorial(n): if n == 1: #base case return 1 else: return n * factorial(n-1) #recursive call factorial(4)
Here the base case of the function is checking whether the number is 1 or not. If not 1, we will multiply the current number n with factorial of n-1 through recursive call of the function. The output will be 24.
Recursion has many more applications in the programming area. You will find out as you go. But first, understand the topic thoroughly and keep practicing.