<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Talk Binary &#187; Base Cases</title>
	<atom:link href="http://talkbinary.com/tag/base-cases/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C++ Recursion</title>
		<link>http://talkbinary.com/programming/c/c-recursion/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 16:54:41 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[activation records]]></category>
		<category><![CDATA[Base Cases]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[Infinite Recursion]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive function]]></category>
		<category><![CDATA[Runtime stack]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2813</guid>
		<description><![CDATA[Recursion in C++ is a function with a set of rules designed to reduce a problem into a smaller one called a base case. This may be referred to as the divide and conquer method since the idea is to solve a complex problem by solving the smaller instances of it. A classic example is<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>Recursion in C++ is a function with a set of rules designed to reduce a problem into a smaller one called a base case. This may be referred to as the <em>divide and conquer</em> method since the idea is to solve a complex problem by solving the smaller instances of it. A classic example is the factorial function, which we&#8217;ll discuss later. </p>
<h3 id="section-1">Recursive function that displays numbers</h3>
<p>For our first example, let&#8217;s create a recursive function that displays a number. In order to do this, we create a function that prints a number to output, then we call itself.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> printnum<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
     printnum<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Recursive call</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Unfortunately, the previous example displays behavior known as <em>infinite recursion</em> on any call. Depending on the environment you are programming in, you might receive a <strong>segmentation fault</strong>. This happens because each <em>activation record</em> of a function is pushed onto the run-time stack. When you have a function that runs indefinitely, you are bound to run out of space.<br />
<span id="more-2813"></span><br />
<center><div id="attachment_2822" class="wp-caption aligncenter" style="width: 144px"><a href="http://talkbinary.com/wp-content/uploads/2010/07/RecursionInfinite.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/07/RecursionInfinite-134x300.jpg" alt="" title="RecursionInfinite" width="134" height="300" class="size-medium wp-image-2822" /></a><p class="wp-caption-text">Example of Infinite Recursion</p></div></center></p>
<h3 id="section-2">Base Cases in Recursion</h3>
<p>In order to eliminate <em>infinite recursion</em> we introduce a <em>base case</em>. A <strong>base case</strong> is a condition that determines when to terminate a recursive function. For our previous example, we&#8217;ll introduce the following base case and also make sure we are reducing our input into a smaller case each call (otherwise, we&#8217;d never reach our base case).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> printnum<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #666666;">// Base case</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> 
          <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Recursive call that reduces input into a smaller case</span>
     printnum<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Below is an example of the output of executing such a function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">printnum<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">5</span> <span style="color: #0000dd;">4</span> <span style="color: #0000dd;">3</span> <span style="color: #0000dd;">2</span> <span style="color: #0000dd;">1</span></pre></div></div>

<p><center><div id="attachment_2823" class="wp-caption aligncenter" style="width: 301px"><a href="http://talkbinary.com/wp-content/uploads/2010/07/RecursionPrintNum.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/07/RecursionPrintNum-291x300.jpg" alt="" title="RecursionPrintNum" width="291" height="300" class="size-medium wp-image-2823" /></a><p class="wp-caption-text">Example of printnum(3) and output to the console</p></div></center></p>
<p>Now, what happens if we execute <em>printnum(-2)</em>? We will never reach our base case and thus receive <em>infinite recursion</em>. In order to avoid this behavior, its essential that we create robust base cases that allow our recursive functions to terminate. </p>
<p>The following is the modified version of our recursive function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> printnum<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #666666;">// Improved base case </span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> 
          <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Recursive call that reduces input into a smaller case</span>
     printnum<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span> 
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3 id="section-3">Things to remember about C++ Recursion</h3>
<p>Below are some things that you should take into consideration when solving recursive problems in C++.</p>
<blockquote><ul>
<li>Recursion in C++ is a very difficult subject. It takes dedication in order to understand and master the subject.</li>
<li>The best way to solve recursive functions is to write down the problem on a piece of paper and identify how you are going to break down the problem into a smaller instance and when to terminate (base cases).</li>
<li>A good way to visualize recursion is to draw the <em>activation records</em> by drawing the run time stack of a simple recursive call</li>
</ul>
</blockquote>
<h3 id="section-4">Going further with C++ Recursion</h3>
<p>Now that you know the basic concept, the best way to learn recursion is by solving problems.</p>
<p>Next: <a href="http://talkbinary.com/programming/c/recursion-examples/">C++ Recursion Examples</a></p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-02-11 19:40:23 -->
