ctorial program, the recursion ends when the ’n’ value reaches ‘0’. That is our <b>Base Condition</b></p><div id="15ee"><pre><span class="hljs-attribute">if</span> n == <span class="hljs-number">0</span>:
<span class="hljs-attribute">return</span> <span class="hljs-number">1</span></pre></div><h2 id="862b">2. Recursive Call</h2><p id="1e7b">In the factorial program, the function ‘factorial’ recursively calls itself by decreasing the ‘n’ value by 1.</p><p id="a106">Each time the number multiplies with the factorial of the number below it until it reaches ‘0’.</p><div id="a9b7"><pre><span class="hljs-variable"><span class="hljs-keyword">else</span></span>:
<span class="hljs-function"><span class="hljs-title">return</span>(<span class="hljs-variable">n</span>*<span class="hljs-title">factorial</span>(<span class="hljs-variable">n</span>-<span class="hljs-number">1</span>))</span></pre></div><h2 id="1284">3. Action</h2><p id="cb85">Action in a Recursive Function is basically, what is the operation you are performing in the program to get the desired output.</p><div id="9852"><pre><span class="hljs-built_in">n</span>*factorial(<span class="hljs-built_in">n</span>-<span class="hljs-number">1</span>)</pre></div><p id="b0de">In the code, we are multiplying the current number with the factorial of the previous number. The action part here is multiplying with the number ‘n’.</p><p id="2ed8">So, it is imperative to follow these 3 rules when we are writing a Recursive Function.</p><h2 id="d27d">Advantages of Recursive Functions</h2><ol><li>If we wants to make our code look elegant and neat, recursive functions make sure of that.</li><li>A complex operation can be divided into similar sub-problems using recursion.</li><li>Recursive Functions are more useful in ‘Sequence Generation’ problems.</li></ol><h2 id="241c">Disadvantages of Recursive Functions</h2><ol><li>It is hard to debug recursive functions.</li><li>Sometimes the logic behind recursive function is little difficult to follow.</li><li>They takes up a lot of space and time while executing.</li></ol><p id="185d">Before we wind up, let’s take a look at another example for better understanding.</p><h2 id="bc14">Fibonacci</h2><p id="8c3c">We all know what is a Fibonacci series looks like, for those of you who are not familiar with it please click <a href="https://en.wikipedia.org/wiki/Fibonacci_number">here</a>.</p><p id="7b49">The Python Code for Fibonacci series without using the recursive function is as follows</p>
<figure id="bbc5">
<div>
<div>
<iframe class="gist-iframe" src="/gist/Kaushik-Varma/14a0854cc1f3d6faf51259e8a9d8d778.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="97bf">Now, let’s see how we can modify the same code using recursive function.</p>
<figure id="cfe7">
<div>
<div>
<iframe class="gist-iframe" src="/gist/Kaushik-Varma/8e1e5faae722640275b3fd31e31270c7.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="1cc2">Let’s check the previously discussed 3 rules in the above program</p><ol><li><b>Base Condition</b></li></ol><p id="0b0f">The base condition here is, if the number is less than or equal to 0, we get out of the function.</p><div id="eb2e"><pre><span class="hljs-built_in">if</span> <span class="hljs-built_in">n</span> <= <span class=
Options
"hljs-symbol">1:</span>
return <span class="hljs-built_in">n</span></pre></div><p id="b2b7">2. <b>Recursive Call</b></p><p id="800b">In the else part of the function we are calling the fibaonacci function again.</p><div id="d785"><pre><span class="hljs-variable"><span class="hljs-keyword">else</span></span>:
<span class="hljs-function"><span class="hljs-title">return</span>(<span class="hljs-title">fibonacci</span>(<span class="hljs-variable">n</span>-<span class="hljs-number">1</span>) + <span class="hljs-title">fibonacci</span>(<span class="hljs-variable">n</span>-<span class="hljs-number">2</span>))</span></pre></div><p id="22f5">3. <b>Action</b></p><p id="635f">The action part here is, adding the previous two elements.</p><div id="7fde"><pre><span class="hljs-built_in">fibonacci</span>(n-<span class="hljs-number">1</span>) + <span class="hljs-built_in">fibonacci</span>(n-<span class="hljs-number">2</span>)</pre></div><h1 id="cf54">Conclusion</h1><p id="8c17">Like I already mentioned, recursive functions make our program more elegant and it’s also easy to write once you get the hang of it, but they have their share of drawbacks too.</p><p id="330e">So, use them wisely and make sure you define the base condition.</p><p id="1420"><b>Thank you for reading</b> and <b>Happy Coding!</b></p><p id="a703">If you are interested in more of my writing check out my previous articles on Python</p><ol><li><a href="https://readmedium.com/python-problems-for-basics-reference-swapping-factorial-reverse-digits-pattern-print-241dde763c74"><b>Python: Problems for Basics Reference — Swapping, Factorial, Reverse Digits, Pattern Print</b></a></li><li><a href="https://readmedium.com/python-programs-to-check-for-armstrong-number-n-digit-and-fenced-matrix-bc84a1bf32aa"><b>Python Programs to check for Armstrong Number (n digit) and Fenced Matrix</b></a></li></ol><h1 id="f74b">References</h1><ul><li><b>Python Function Recursion</b>: <a href="https://www.w3schools.com/python/gloss_python_function_recursion.asp">https://www.w3schools.com/python/gloss_python_function_recursion.asp</a></li><li><b>Python Recursion:</b> <a href="https://www.programiz.com/python-programming/recursion">https://www.programiz.com/python-programming/recursion</a></li><li><b>Thinking Recursively in Python</b>: <a href="https://realpython.com/python-thinking-recursively/">https://realpython.com/python-thinking-recursively/</a></li><li><b>Recursive Functions</b>: <a href="https://www.python-course.eu/recursive_functions.php">https://www.python-course.eu/recursive_functions.php</a></li><li><b>Python Recursion</b>: <a href="https://beginnersbook.com/2018/02/python-recursion/">https://beginnersbook.com/2018/02/python-recursion/</a></li><li><b>Python — Recursion: <a href="https://www.tutorialspoint.com/python_data_structure/python_recursion.htm"></a></b><a href="https://www.tutorialspoint.com/python_data_structure/python_recursion.htm">https://www.tutorialspoint.com/python_data_structure/python_recursion.htm</a></li><li><b>Recursion in Python: <a href="https://www.tutorialsteacher.com/python/recursion-in-python"></a></b><a href="https://www.tutorialsteacher.com/python/recursion-in-python">https://realpython.com/binary-search-python/</a></li><li><b>Recursion: <a href="https://en.wikipedia.org/wiki/Recursion"></a></b><a href="https://en.wikipedia.org/wiki/Recursion">https://en.wikipedia.org/wiki/Recursion</a></li><li><b>Fibonacci number: <a href="https://en.wikipedia.org/wiki/Fibonacci_number"></a></b><a href="https://en.wikipedia.org/wiki/Fibonacci_number">https://en.wikipedia.org/wiki/Fibonacci_number</a></li></ul></article></body>
Recursion occurs when a thing is defined in terms of itself. The most common application of Recursion is in Mathematics and Computer Science.
What is Recursive Function in Python?
In Python, we know that a function can call other functions. It is also possible for the function to call itself. These types of functions are known as Recursive Function.
It is very easy to write these functions but it is quite possible that the function which we have written calls itself and never terminates. Also, we have to be mindful of the amount of space and memory it uses. Albeit, when written correctly Recursive Functions are a very efficient and more elegant approach to programming.
Let’s see with some examples of how one can use Recursive Functions to their advantage.
Factorial
Many of you know, how to write a python code to find a Factorial for a given number. Let’s see the code for Factorial for those who are not familiar with it.
In the above code, we are basically multiplying the number in a for loop and adding that value to the variable every time.
Like this
f = 1for i in range(1,n+1)
f = f*i
Now, for the same Factorial problem, if we use recursion function, the python code will be as follows
In the recursive code, we created a function ‘factorial’ which takes one parameter ’n’. Now, inside that function, we are performing an operation ‘n*factorial(n-1)’, in which we are calling the same factorial function which takes the input as ‘n-1’. This process of function calling keeps on repeating until the ’n’ value reaches ‘0’.
Every Recursive Function must follow 3 main conditions.
Base Case or Base Condition
Recursive Call
Action
1. Base Condition
Every Recursive Function must have a Base Condition, which helps the function to stop the recursion. Without a base condition, the function calls itself infinitely.
In the above Factorial program, the recursion ends when the ’n’ value reaches ‘0’. That is our Base Condition
if n == 0:
return1
2. Recursive Call
In the factorial program, the function ‘factorial’ recursively calls itself by decreasing the ‘n’ value by 1.
Each time the number multiplies with the factorial of the number below it until it reaches ‘0’.
else:
return(n*factorial(n-1))
3. Action
Action in a Recursive Function is basically, what is the operation you are performing in the program to get the desired output.
n*factorial(n-1)
In the code, we are multiplying the current number with the factorial of the previous number. The action part here is multiplying with the number ‘n’.
So, it is imperative to follow these 3 rules when we are writing a Recursive Function.
Advantages of Recursive Functions
If we wants to make our code look elegant and neat, recursive functions make sure of that.
A complex operation can be divided into similar sub-problems using recursion.
Recursive Functions are more useful in ‘Sequence Generation’ problems.
Disadvantages of Recursive Functions
It is hard to debug recursive functions.
Sometimes the logic behind recursive function is little difficult to follow.
They takes up a lot of space and time while executing.
Before we wind up, let’s take a look at another example for better understanding.
Fibonacci
We all know what is a Fibonacci series looks like, for those of you who are not familiar with it please click here.
The Python Code for Fibonacci series without using the recursive function is as follows
Now, let’s see how we can modify the same code using recursive function.
Let’s check the previously discussed 3 rules in the above program
Base Condition
The base condition here is, if the number is less than or equal to 0, we get out of the function.
ifn <= 1:
return n
2. Recursive Call
In the else part of the function we are calling the fibaonacci function again.
else:
return(fibonacci(n-1) + fibonacci(n-2))
3. Action
The action part here is, adding the previous two elements.
fibonacci(n-1) + fibonacci(n-2)
Conclusion
Like I already mentioned, recursive functions make our program more elegant and it’s also easy to write once you get the hang of it, but they have their share of drawbacks too.
So, use them wisely and make sure you define the base condition.
Thank you for reading and Happy Coding!
If you are interested in more of my writing check out my previous articles on Python