<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aoi</title>
    <description>The latest articles on DEV Community by Aoi (@nandesh).</description>
    <link>https://dev.to/nandesh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1101429%2F0e20ea0b-a4f9-435e-9bd2-14696b832482.jpeg</url>
      <title>DEV Community: Aoi</title>
      <link>https://dev.to/nandesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nandesh"/>
    <language>en</language>
    <item>
      <title>Understanding Recursion! The easy way.</title>
      <dc:creator>Aoi</dc:creator>
      <pubDate>Fri, 16 Jun 2023 15:08:45 +0000</pubDate>
      <link>https://dev.to/nandesh/understanding-recursion-the-easy-way-1b7e</link>
      <guid>https://dev.to/nandesh/understanding-recursion-the-easy-way-1b7e</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Recursion: It's like falling down a rabbit hole and realizing the rabbit hole is inside another rabbit hole, and it just keeps going&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's make this hard topic easy for our brains. How do you add these numbers? 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = ? &lt;br&gt;
You might say we simply add them together or type them out on a calculator. If you thought so you are correct but what if we wanna write it down as a function? Here comes recursion and together we will go through the simple steps to write our own unique function. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;If the Math Genius within you said "Oh, can't you just use an AP?" Then we will talk about that at the end of this post.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Simple Process
&lt;/h2&gt;

&lt;p&gt;Before we begin lets name our function &lt;code&gt;fn(n)&lt;/code&gt; where "n" is the starting number. So in theory&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn(9) = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;What if we reduce that n to 8. Now our function gives&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn(8) = 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Notice something? &lt;code&gt;fn(8)&lt;/code&gt; contains a part of &lt;code&gt;fn(9)&lt;/code&gt;. So lets just replace that.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn(9) = 9 + fn(8)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;What about fn(8)? If you think about it&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn(8) = 8 + fn(7)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Don't you see a pattern here? In both of them, we are simply adding our number (9) to the function of number before it. So can we write that as&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn(n) = n + fn(n-1)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here (n-1) is the number before it.&lt;/p&gt;

&lt;p&gt;By this, we have successfully divided our problem into a smaller problem. Step 1 complete.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the End
&lt;/h2&gt;

&lt;p&gt;Now we know&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn(9) = 9 + fn(8)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;we need to find an end to it. In our series we assumed that the ending number is always 1. So what will &lt;code&gt;fn(1)&lt;/code&gt; be? Since our last number is one, we have to stop here. Hence&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fn(1) = 1&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Making it work (hopefully)
&lt;/h2&gt;

&lt;p&gt;Begin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn(n) = {
  if ( n = 1 ){
    return 1  
  }  else {
    return n + fn(n-1)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and that's it. We have our function. Time to run it in our heads. (I am just gonna type it out ok?)&lt;/p&gt;

&lt;p&gt;We should take a smaller number so it's simpler. So let n be 3.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;fn(3) = 3 + fn(3-1)&lt;/li&gt;
&lt;li&gt;fn(3) = 3 + 2 + fn(1)&lt;/li&gt;
&lt;li&gt;fn(3) = 3 + 2 + 1&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So it works. (Nice) Now you know how to write recursive function which call themselves but it's always good to see the problems with any good techniques.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems
&lt;/h2&gt;

&lt;p&gt;What is we do fn(-1). Try it yourself, did you get an output?&lt;br&gt;
You won't, and that is because we never stop, our stop condition only checks if the number is 1 but we never thought of negative numbers. You might think we can just make it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn(n) = {
  if ( n &amp;lt;= 1 ){  //Triggers if n is less than or equal to 1
    return 1  
  }  else {
    return n + fn(n-1)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our case this was a simple example, but when we try to make more complex functions, we might miss out on a rare one time end case which we never thought of. In this case our function will be stuck calling itself over the over again without stop. (Which is really bad).&lt;/p&gt;

&lt;p&gt;Hence many companies making robust software actually avoid using recursive functions. &lt;em&gt;They don't want their software to be stuck calling itself instead of collecting user data.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most of the time there are even better ways to solve these problems without recursion. You might have studied in your school and concept of Arithmetic Progression (AP). We can use that to remake our function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn(n) = n * (n + 1) / 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a much faster and simpler way to solve this problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  The End
&lt;/h2&gt;

&lt;p&gt;So now you know recursion, which you can use to be a better programmer &lt;em&gt;(Or impress your interviewer)&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>coding</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
