<?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; display function</title>
	<atom:link href="http://talkbinary.com/tag/display-function/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>Introduction to Recursion in C++</title>
		<link>http://talkbinary.com/programming/c/introduction-to-recursion-in-c/</link>
		<comments>http://talkbinary.com/programming/c/introduction-to-recursion-in-c/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 23:18:23 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[activation records]]></category>
		<category><![CDATA[base case]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[display function]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[terminating condition]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=1572</guid>
		<description><![CDATA[Recursion in C++ is simply a function that calls itself that terminates when a base case is met. ]]></description>
			<content:encoded><![CDATA[<p>Recursion in C++ is simply a function that calls itself that terminates when a base case is met. It&#8217;s very useful to learn how recursion for a couple of reasons.</p>
<ul>
<li>It&#8217;s easier to solve certain problems with recursion as the resulting code is usually shorter</li>
<li>Sometimes its the only way</li>
</ul>
<h3 id="section-1">Displaying numbers recursively</h3>
<p>So what&#8217;s an example of a recursive function? Let&#8217;s make one. Let&#8217;s start by creating a function that displays a number (it isn&#8217;t the best example, but it&#8217;s good enough to introduce you to recursion). </p>
<h4 id="section-2">Display Function</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> display<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>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/>All this function does is simply print the number passed in. </p>
<h4 id="section-3">Recursive Display Function</h4>
<p>Now let&#8217;s have this function call itself but lets decrement the value being displayed each time.</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> display<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>
     display<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: #666666;">//Will call display but with (n-1)</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So let&#8217;s say we executed display(10)</p>
<p>What would this display? It would display 10 9 8 7 6 5 4&#8230;and would never stop. Why? We never told the function when to terminate. This will either cause your program to run until you kill it, or may cause it to segmentation fault. In order to fix this, this calls for the addition of <strong>base case</strong>. <span id="more-1572"></span></p>
<blockquote><h3 id="section-4">Base Cases in Recursion</h3>
<p>In order for your recursive function to terminate you <strong>always</strong> need a base case. You may have multiple base cases. </p></blockquote>
<h4 id="section-5">Recursive Display Function with a Base Case</h4>
<p>Let&#8217;s make the function terminate when n is 0. Or better yet, let&#8217;s make it terminate when n is less than or equal to 0. (Why do you think this is better?)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> display<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: #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>
     display<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: #666666;">//Will call display but with (n-1)</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So now, if we execute display(10), what would it display? </p>
<pre lang="c++">10 9 8 7 6 5 4 3 2 1 </pre>
<h3 id="section-6">How Recursion works in C++</h3>
<p>So how does recursion work in C++? Each <strong>function</strong> creates an activation record on the <strong>stack</strong>. As a function gets called, it gets added to the top of the stack. Until the function is terminated, is then taken off the stack. A visual example for our display function is below.</p>
<p><center><div id="attachment_1594" class="wp-caption alignnone" style="width: 245px"><a href="http://talkbinary.com/wp-content/uploads/2009/02/recursion.jpg"><img src="http://talkbinary.com/wp-content/uploads/2009/02/recursion-235x300.jpg" alt="Click to Enlarge" title="recursion" width="235" height="300" class="size-medium wp-image-1594" /></a><p class="wp-caption-text">(Click to Enlarge) </p></div></center></p>
<p>The first function called was display(10) which is the activation record seen at the bottom of our stack. If you notice, the following function keep getting called as long as the base case isn&#8217;t met. Up until n = -1, then the activation record of the functions are popped off.  </p>
<h3 id="section-7">Comments on Recursion</h3>
<p>What is shown previously is a very basic recursive algorithm. More recursion techniques will be discussed later. So even if you don&#8217;t understand the purpose of recursion just yet, don&#8217;t hesitate. You&#8217;ll understand why it&#8217;s important when you understand recursion is the only way to do certain problems. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/introduction-to-recursion-in-c/feed/</wfw:commentRss>
		<slash:comments>10</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 22:05:14 -->
