<?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: AhlemKaabi</title>
    <description>The latest articles on DEV Community by AhlemKaabi (@ahlemkaabi).</description>
    <link>https://dev.to/ahlemkaabi</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%2F661277%2F2de9fd31-d3a9-4d33-a40d-aa1aa5f25b1e.png</url>
      <title>DEV Community: AhlemKaabi</title>
      <link>https://dev.to/ahlemkaabi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahlemkaabi"/>
    <language>en</language>
    <item>
      <title>Recursion and Stack for Beginners</title>
      <dc:creator>AhlemKaabi</dc:creator>
      <pubDate>Sun, 04 Jul 2021 22:20:09 +0000</pubDate>
      <link>https://dev.to/ahlemkaabi/recursion-and-stack-for-beginners-4jca</link>
      <guid>https://dev.to/ahlemkaabi/recursion-and-stack-for-beginners-4jca</guid>
      <description>&lt;p&gt;Hello everyone!&lt;/p&gt;

&lt;p&gt;Today’s topic is about the recursion process and what happens at the stack?&lt;/p&gt;

&lt;p&gt;Considering the function __pow_recursion(float x, float y) that returns the power of a given number (x^y) using the Recursion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;__pow_recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;   
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_pow_recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&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="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_pow_recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&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="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Recursion function is a nested function, which means it calls another function. But what makes it special that it calls itself several times until it reaches the ending conditions (if statements of our case).&lt;/p&gt;

&lt;p&gt;The key to understanding any code is to follow the instructions one by one, we will do the same thing with our __pow_recursion() function, taking x = 5 and y = 3.&lt;/p&gt;

&lt;p&gt;Now let’s follow the arrows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UDyrhdwx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dywqtd15wa26k0rnuwrs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UDyrhdwx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dywqtd15wa26k0rnuwrs.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the __pow_recursion(5, 3) will return [the return of the function __pow_recursion(5, 2) multiple 5 ].&lt;/p&gt;

&lt;p&gt;Then the program will wait until it gets the return of the function __pow_recursion(5,2), that means the program will call to the same function with different arguments, x as 5 and y as 2.&lt;/p&gt;

&lt;p&gt;And again, the __pow_recursion(5, 2) will return [the return of the function __pow_recursion(5, 1) multiple 5 ].&lt;/p&gt;

&lt;p&gt;Also again, the program will wait until it gets the return of the function __pow_recursion(5,1), now don’t lose your concentration because the program will again call the same function with different arguments, x as 5 and y as 1.&lt;/p&gt;

&lt;p&gt;We almost reach the end of our recursion!&lt;br&gt;
The __pow_recursion(5, 1) will return [the return of the function __pow_recursion(5, 0) multiple 5]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nf"&gt;__pow_recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/*__pow_recursion(5, 0)*/&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;   
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cm"&gt;/*true*/&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/*our return is 1*/&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_pow_recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&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="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_pow_recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&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="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the return of the function(5, 0) is 1.&lt;/p&gt;

&lt;p&gt;There is no more calling to our function! The recursion is done! BUT!&lt;br&gt;
Let’s get back to our functions, they are waiting! And give one by one the return needed!&lt;br&gt;
the __pow_recursion(5, 1) will return 1 * 5 = 5&lt;br&gt;
the __pow_recursion(5, 2) will return 5 * 5 = 25&lt;br&gt;
the __pow_recursion(5, 3) will return 25 * 5 = 125&lt;/p&gt;

&lt;p&gt;And the execution is DONE!&lt;/p&gt;

&lt;p&gt;Now, what happens at the stack? And what is it in the first place?&lt;/p&gt;

&lt;p&gt;First, the stack is a data structure with LIFO property that means the last data kept in is the first to go out to use/delete…&lt;br&gt;
Second, a part of the memory took the name of that data structure ‘stack’ because it uses the same property to keep the data!&lt;br&gt;
But what data?&lt;br&gt;
When the program is executed, the variables, function calls, text codes… kept on the stack!&lt;br&gt;
Every instruction in our code/function ran will have a place on the stack, and when it is done/completed, it goes forever. Unlike another type of memory which is the heap, that keeps data forever! And for that, we have to manage that.&lt;br&gt;
The recursion uses the stack for its LIFO property, which makes a function remember its “caller” function, and know to whom it will return its output!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b_RTDlfA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53106f0czpaxzcdvot8m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b_RTDlfA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53106f0czpaxzcdvot8m.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoyed reading and have learned something new!&lt;/p&gt;

</description>
      <category>recursion</category>
      <category>c</category>
      <category>stack</category>
    </item>
  </channel>
</rss>
