<?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: sachin suman</title>
    <description>The latest articles on DEV Community by sachin suman (@sachin_suman_d061f96779fe).</description>
    <link>https://dev.to/sachin_suman_d061f96779fe</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%2F1998865%2Fafb8c45d-44b8-47e2-800a-2b048e6e4438.png</url>
      <title>DEV Community: sachin suman</title>
      <link>https://dev.to/sachin_suman_d061f96779fe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sachin_suman_d061f96779fe"/>
    <language>en</language>
    <item>
      <title>C++ Recursion</title>
      <dc:creator>sachin suman</dc:creator>
      <pubDate>Fri, 30 Aug 2024 11:31:16 +0000</pubDate>
      <link>https://dev.to/sachin_suman_d061f96779fe/c-recursion-5e6d</link>
      <guid>https://dev.to/sachin_suman_d061f96779fe/c-recursion-5e6d</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;DEFINING RECURSION&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;When a function is called within the same function, it is known as recursion in C++. The function which calls the same function is known as a recursive function. Recursion is very useful concept as it empowers a function to solve problems by breaking them down into smaller instances of the same problem.&lt;/p&gt;

&lt;p&gt;The figure below shows how recursion works by calling itself over and over again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foljf7d1kg1rdx8hps9j1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foljf7d1kg1rdx8hps9j1.png" alt="Image description" width="800" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  BASE CONDITION
&lt;/h2&gt;

&lt;p&gt;The base condition is the condition that is used to terminate the recursion. The recursive function will keep calling itself till the base condition is satisfied.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;br&gt;
The recursion continues until some condition is met.&lt;br&gt;
To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and the other doesn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  EXAMPLE OF RECURSION PROGRAM
&lt;/h2&gt;

&lt;p&gt;// Factorial of n = 1*2*3*...*n&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;int factorial(int);&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
    int n, result;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cout &amp;lt;&amp;lt; "Enter a non-negative number: ";
cin &amp;gt;&amp;gt; n;

result = factorial(n);
cout &amp;lt;&amp;lt; "Factorial of " &amp;lt;&amp;lt; n &amp;lt;&amp;lt; " = " &amp;lt;&amp;lt; result;
return 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;int factorial(int n) {&lt;br&gt;
    if (n &amp;gt; 1) {&lt;br&gt;
        return n * factorial(n - 1);&lt;br&gt;
    } else {&lt;br&gt;
        return 1;&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;OUTPUT:- Enter a non-negative number: 4&lt;br&gt;
Factorial of 4 = 24&lt;/p&gt;

&lt;p&gt;EXPLANATION:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepz5gjff4wqpodzviar1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepz5gjff4wqpodzviar1.png" alt="Image description" width="800" height="1124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Advantages of C++ Recursion&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It makes our code shorter and cleaner.&lt;/li&gt;
&lt;li&gt;Recursion is required in problems concerning data structures and advanced algorithms, such as Graph and Tree Traversal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages of C++ Recursion&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It takes a lot of stack space compared to an iterative program.&lt;/li&gt;
&lt;li&gt;It uses more processor time.&lt;/li&gt;
&lt;li&gt;It can be more difficult to debug compared to an equivalent iterative program.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>learning</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
