<?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: hy86592</title>
    <description>The latest articles on DEV Community by hy86592 (@hy86592).</description>
    <link>https://dev.to/hy86592</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%2F71347%2F62ee2397-61c4-48ef-beae-3a57d84a442d.jpg</url>
      <title>DEV Community: hy86592</title>
      <link>https://dev.to/hy86592</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hy86592"/>
    <language>en</language>
    <item>
      <title>Finally Understanding Recursive Functions</title>
      <dc:creator>hy86592</dc:creator>
      <pubDate>Tue, 17 May 2022 02:57:15 +0000</pubDate>
      <link>https://dev.to/hy86592/finally-understanding-recursive-functions-2hjb</link>
      <guid>https://dev.to/hy86592/finally-understanding-recursive-functions-2hjb</guid>
      <description>&lt;p&gt;I used to have a hard time wrapping my mind around recursive function. Today, I learned 3 characteristics about recursion that makes them easy to understand. &lt;/p&gt;

&lt;p&gt;The 3 characteristics of recursive functions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Recursive functions &lt;strong&gt;have a state&lt;/strong&gt; that is represented by a parameter value. &lt;/li&gt;
&lt;li&gt;When calling the inner function, ensure that the parameter (state) &lt;strong&gt;must be modified in some way&lt;/strong&gt; that brings it closer to the 'break condition' and &lt;strong&gt;eventually meet it&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Set the &lt;strong&gt;'break condition'&lt;/strong&gt; for which the inner function is not called again (i.e. breaking the recursion).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function #1
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run until the stack overflows because it's just running itself continuously.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function #2
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;     &lt;span class="c1"&gt;# the state is represented by the value n
&lt;/span&gt;  &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# no break condition; no change of state (n)
&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will also run until the stack overflows because there is no breaking condition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function #3
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="c1"&gt;# break condition (always unsatisfied)
&lt;/span&gt;    &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="c1"&gt;# no change of state (n)
&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will also run until the stack overflows because the state (n) always satisfies the condition for running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function #4
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="c1"&gt;# break condition (always unsatisfied)
&lt;/span&gt;    &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="c1"&gt;# no change of state
&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will never run a second time because the state (n) never meets the conditions for running again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function #5
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="c1"&gt;# breaking condition (satisfied until the state meets breaking condition)
&lt;/span&gt;    &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# continuous change of state
&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run recursively until the breaking condition is met.&lt;/p&gt;

&lt;p&gt;There you have it. A recursive function has a state that is represented by a parameter value and requires a change of state (n) which eventually meets the 'breaking condition' of the function.&lt;/p&gt;

&lt;p&gt;I hope this helps. &lt;/p&gt;

</description>
      <category>recursion</category>
      <category>python</category>
      <category>functions</category>
    </item>
  </channel>
</rss>
