When a function call itself is knows as recursion. Recursion works like loop but sometimes it makes more sense to use recursion than loop. You can convert any loop to recursion.
Here is how recursion works. A recursive function calls itself. As you you'd imagine such a process would repeat indefinitely if not stopped by some condition. This condition is known as base condition. A base condition is must in every recursive programs otherwise it will continue to execute forever like an infinite loop.
Overview of how recursive function works:
Recursive function is called by some external code.
If the base condition is met then the program do something meaningful and exits.
Otherwise, function does some required processing and then call itself to continue recursion. Here is an example of recursive function used to calculate factorial.
Factorial is denoted by number followed by (!
) sign i.e 4!
.
For e.g:
4! = 4 * 3 * 2 * 1 2! = 2 * 1 0! = 1
Here is an example
def fact(n): if n \== 0: return 1 else: return n * fact(n-1)
print(fact(0)) print(fact(5))
Expected Output:
Now try to execute the above function like this:
You will get:
RuntimeError: maximum recursion depth exceeded in comparison
This happens because python stop calling recursive function after 1000
calls by default. To change this behavior you need to amend the code as follows.
import sys sys.setrecursionlimit(3000)
def fact(n): if n \== 0: return 1 else: return n * fact(n-1)
print(fact(2000))
A recursive function is a that calls itself until it doesn’t.
The following fn()
function is a recursive function because it has a call to itself:
In the fn()
function, the #...
means other code.
Also, a recursive function needs to have a condition to stop calling itself. So you need to add an like this:
Typically, you use a recursive function to divide a big problem that’s difficult to solve into smaller problems that are easier-to-solve.
In programming, you’ll often find the recursive functions used in data structures and algorithms like trees, graphs, and binary searches.
Let’s take some examples of using the Python recursive functions.
Suppose you need to develop a countdown function that counts down from a specified number to zero.
For example, if you call the function that counts down from 3, it’ll show the following output:
The following defines the count_down()
function:
If you call the count_down()
function now:
…it’ll shows only the number 3.
To show the number 3, 2 and 1, you need to:
First, call the count_down(3)
to show the number 3.
Second, call the count_down(2)
to show the number 2.
Finally, call the count_down(1)
to show the number 1.
In order to do so, inside the count_down()
function, you’ll need to define a logic to call the function count_down()
with argument 2, and 1.
To do it, you need to make the count_down()
function recursive.
The following defines a recursive count_down()
function and calls it by passing the number 3:
If you execute the program, you’ll see the following error:
The reason is that the count_down()
calls itself indefinitely until the system stops it.
Since you need to stop counting down the number reaches zero. To do so, you add a condition like this:
Output:
In this example, the count_down()
function only calls itself when the next number is greater than zero. In other words, if the next number is zero, it stops calling itself.
Output:
To apply the recursion technique, you can calculate the sum of the sequence from 1 to n as follows:
sum(n) = n + sum(n-1)
sum(n-1) = n-1 + sum(n-2)
…
sum(0) = 0
The sum()
function keeps calling itself as long as its argument is greater than zero.
The following defines the recursive version of the sum()
function:
As you can see, the recursive function is much shorter and more readable.
A recursive function is a function that calls itself until it doesn’t.
And a recursive function always has a condition that stops calling itself.
Suppose that you need to calculate a sum of a sequence e.g., from 1 to 100. A simple way to do this is to use a :
If you use the , the sum()
will be even more concise: