<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Promise Nwafor on Medium]]></title>
        <description><![CDATA[Stories by Promise Nwafor on Medium]]></description>
        <link>https://medium.com/@makybeky9?source=rss-e97dc46b1566------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LEm-PtMLlLekcNjeVsS8XQ.jpeg</url>
            <title>Stories by Promise Nwafor on Medium</title>
            <link>https://medium.com/@makybeky9?source=rss-e97dc46b1566------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 08:21:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@makybeky9/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RECURSION]]></title>
            <link>https://medium.com/@makybeky9/recursion-523d7a3b3d89?source=rss-e97dc46b1566------2</link>
            <guid isPermaLink="false">https://medium.com/p/523d7a3b3d89</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Promise Nwafor]]></dc:creator>
            <pubDate>Thu, 18 Jun 2020 10:45:07 GMT</pubDate>
            <atom:updated>2020-06-19T08:21:07.654Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>RECURSION</strong></p><p>Recursion is a method of solving problems where the solution depends on solutions to smaller instances of the same problem. I know that sounds not promising don’t panic, soon it will become much more clear.</p><p>You can simply put that recursion is a process that gives room for a function to call itself from within its own code. The function keeps calling itself until a simple base case (or cases) is reached.</p><p>Take for instance</p><p>You’ve been tasked to babysit for the first time and was asked to feed the baby. You’re to feed this baby till he is full but your problem is how do you know when the baby gets full. So, you were given the following instructions</p><ul><li>While the food contained in the baby bottle is more than 5ml, keep feeding the baby.</li></ul><p>- The base case here is once the food is less than or equal to 5ml (which is the stopping condition)</p><p>- The recursive process (while the food is greater than 5ml) is carried on if the base case is not met yet.</p><p>This is a lay man’s scenario. In programming, computer is the babysitter which is watching out for the base case.</p><p>Here,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*HMS7EXKL9Ep6WaK1GFqtkQ@2x.jpeg" /></figure><p>If the above doesn’t make sense yet, skip it and let’s continue babysitting ☺️, it will make sense soonest;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*b5XhQX7ChicqQrvpLIgA2Q@2x.jpeg" /></figure><p>Each time the function is called inside the code, the number of the argument passed is reduced. That is to say, from our code, anytime we call feedBaby function inside the the root feedBaby we declared, babyFood is reduced by some volume. This is continued down to a point where babyFood deceases to 5ml. Then, the first conditional statement in line 3 is reached, the function will then return “baby is full”. Notice the return placed in line 5, this is to make sure that we break out of the function as soon as the base case is met (baby is full), otherwise, the other conditional statement in line 8 continues to run (ie we keep feeding the baby) which is not good.</p><p>Every recursive function should have a base case (or base cases), else, the web browser would crash as a result of infinite loops or stack overflows that’ll arise from calling a function infinitely.</p><p>Now, revisit the syntax from code 1 above, it should make some sense now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*uFu_1agDckW240wZcOs7DA@2x.jpeg" /></figure><p>Take a look at the function above and let’s break it down below;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*rN_QpZIWKvQZHMepiO4ZBA@2x.jpeg" /></figure><p>Since ‘num’ = 4 and not 1,</p><p>The computer skips the first condition at line 2 and jumps to line 6.</p><p>Line 6 will return</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*pOyCz7ym50gI9uuPExTuTQ@2x.jpeg" /></figure><p>The process is repeated again but now ‘num’ is decreased by 1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*kjv5UmlW3hmleJqMJLLx3A@2x.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*G3Vab_T67rNq3CLhhsMt2g@2x.jpeg" /></figure><p>This process is repeated for when ‘num’ equals 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*f-eDGUqFLBmkAo0j36bWAw@2x.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*aotgmsienLtgS0cLXx62tQ@2x.jpeg" /></figure><p>Then, ‘num’ decreases to 1 which now meets the first condition in line 3. Thus,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*AX4ig6_8TWZmoksJaYwYRQ@2x.jpeg" /></figure><p>It returns 1 and breaks the process.</p><p>The individual numbers returned are then summed up to arrive at 10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*CQfOYXQL0i7owzTyBLM7Wg@2x.jpeg" /></figure><p>Go over these instances till your are comfortable moving forward.</p><p>Now you’re comfortable, let’s solve one real problem on calculating the factorial of a given number.</p><p>A <strong>factorial</strong> is a function that multiplies a number by every number below it. For example 5! (ie, factorial of 5) = 5*4*3*2*1=120</p><p>Using recursion to solve factorial;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*9vKTIoddLJqN2FpKAwa8kQ@2x.jpeg" /></figure><p>Use the instances above to handle any difficulty you may have in writing the factorial function.</p><p>Note that you could solve the same problem using iteration method which is still okay but at times, shorter codes are required for a specific task and that is where recursion comes to play.</p><p>BONUS: using iteration to solve factorial;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*UDSeUg4ffKawodNVPzL8Cg@2x.jpeg" /></figure><p>I hope by now you’ve understood to some extent how to use recursion to solve problems. Give it more time if it’s still confusing for you, it was, for me too till I practiced it further. So, keep practicing and soon you’ll start teaching it to others.</p><p>Happy Learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=523d7a3b3d89" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>