<?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; activation records</title>
	<atom:link href="http://talkbinary.com/tag/activation-records/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Wed, 15 Feb 2012 16:01:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</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>
		<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-05-22 22:57:41 -->
