{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/recursion-for-beginners/","result":{"data":{"markdownRemark":{"id":"4b8fdfb4-2007-5406-a0a7-d04466e0e8df","html":"<h3 id=\"recursion-for-beginners\" style=\"position:relative;\"><a href=\"#recursion-for-beginners\" aria-label=\"recursion for beginners permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Recursion For Beginners</h3>\n<p>What is recursion?</p>\n<blockquote>\n<p><strong>Recursion</strong> is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to <a href=\"https://en.wikipedia.org/wiki/Iteration#Computing\" title=\"Iteration\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">iteration</a>).<a href=\"https://en.wikipedia.org/wiki/Recursion_%28computer_science%29#cite_note-1\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">[1]</a> The approach can be applied to many types of problems, and <a href=\"https://en.wikipedia.org/wiki/Recursion\" title=\"Recursion\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">recursion</a> is one of the central ideas of computer science. — <a href=\"https://en.wikipedia.org/wiki/Recursion_%28computer_science%29\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><em>Wikipedia</em></a></p>\n</blockquote>\n<p>A recursive function is a function which either calls itself or is in a potential cycle of function calls. As the definition specifies, there are two types of recursive functions.</p>\n<ul>\n<li><strong>Linear Recursion:</strong></li>\n</ul>\n<p>In linear recursion a function calls exactly once to itself each time the function is invoked, and grows linearly in proportion to the size of the problem.</p>\n<ul>\n<li><strong>Multiple Recursion:</strong></li>\n</ul>\n<p>Multiple recursion can be treated a generalized form of binary recursion. When a function makes multiple recursive calls possibly more than two, it is called multiple recursion.</p>\n<p>Consider a function which calls itself: we call this type of recursion <strong>linear</strong> recursion. — <a href=\"http://pages.cs.wisc.edu/~calvin/cs110/RECURSION.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Credit</a></p>\n<p>Basically, the recursion solves a problem by breaking the problem itself into smaller pieces.</p>\n<figure>\n<p><img src=\"/media/recursion-for-beginners-0.jpeg\"></p>\n</figure>\n<h3 id=\"writing-recursive-functions\" style=\"position:relative;\"><a href=\"#writing-recursive-functions\" aria-label=\"writing recursive functions permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Writing Recursive Functions</strong></h3>\n<p>A recursive function has the following general form (it is simply a specification of the general function we have seen many times):</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">function factorial(number) {\n    if (number === 1) {\n        return 1;\n    }\n    return number * factorial(number - 1);\n}</code></pre></div>\n<p>For a recursive function to stop calling itself we require some type of stopping condition. If it is not the base case, then we simplify our computation using the general formula.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">undefined</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">undefined</code></pre></div>\n<p>Above are two python functions that make use of the recursion concept. The first which returns the result of a number, <code class=\"language-text\">a</code> raised to the power of a second integer <code class=\"language-text\">b</code>. Line 2 and 4 of the statement define a simple case which returns the simple value for the user and can also be used for breaking out of the loop too. For example, if we want to calculate the power of 2 raised to the power of 3 using our function: <code class=\"language-text\">power(2,3).</code></p>\n<p>The first and second conditions (lines 2 and 4) evaluate to false, when it gets to the else statement on <code class=\"language-text\">line 6</code> it then starts recursion and starts by returning a result that calls the function itself:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">power(2,3) = 2 * power(2,(3-1=2)) = 2 * power(2,2)</code></pre></div>\n<p>In the new function call, both line 2 and 4 both evaluate to false again and then the function calls itself again.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">power(2,2) = 2 * power(2, (2-1=1)) = 2 * power(2,1)</code></pre></div>\n<p>Here, we have broken the original problem into two parts:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">power(2,3) = 2 * power(2,2)</code></pre></div>\n<p>Since <code class=\"language-text\">power(2,2) evaluates to 2 * power(2, 1)</code>, we can redefine the problem statement in a much simpler term:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">power(2,3) = 2 * (2 * power(2,1)</code></pre></div>\n<p>The new function call, <code class=\"language-text\">power(2,1)</code> doesn’t make lines 2 &#x26; 4 evaluate to <code class=\"language-text\">True</code> also so we need to call the function a third time. This time, <code class=\"language-text\">power(2,1)</code> evaluates to:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">power(2,1) = (2 * power(2,1-1) = 2 * power(2,0)</code></pre></div>\n<p>Woah, see how we’ve managed to break down our problem into such small unit. Awesome.</p>\n<p>So we can conveniently simplify<code class=\"language-text\">power(2,3)</code> to</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">2 * (2 * (2 * power(2,0)))</code></pre></div>\n<p>It is easy to solve this problem because <code class=\"language-text\">line 2</code> now evaluates to <code class=\"language-text\">True</code> .</p>\n<p>This means that <code class=\"language-text\">power(2,0) == 1</code> . We now have a statement that looks like this <code class=\"language-text\">power(2,3) = 2 * (2 * (2 * 1)) = 8</code>.</p>\n<p>Awesome stuff! We’ve broken our problem down and solved the problem while doing that.</p>\n<p>This is <code class=\"language-text\">recursion</code> broken down into simple units. The javascript equivalent of the above python function is shown below:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">undefined</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">undefined</code></pre></div>\n<p>I hope you now have a better understanding of what a recursive function is and how to write one in any language.</p>\n<p>Go Devs!</p>","fields":{"slug":"/posts/recursion-for-beginners/","tagSlugs":["/tag/java-script/","/tag/programming/","/tag/python/","/tag/recursive-functions/","/tag/andela/"]},"frontmatter":{"date":"2017-04-20T13:40:23.283Z","description":"A recursive function is a function which either calls itself or is in a potential cycle of function calls. As the definition specifies, there are two types of recursive functions. In linear…","tags":["JavaScript","Programming","Python","Recursive Functions","Andela"],"title":"Recursion For Beginners","socialImage":null}}},"pageContext":{"slug":"/posts/recursion-for-beginners/"}},"staticQueryHashes":["251939775","401334301","4120999787"]}