<?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; c++ factorial</title>
	<atom:link href="http://talkbinary.com/tag/c-factorial/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>Tail Recursion</title>
		<link>http://talkbinary.com/programming/c/c-tail-recursion/</link>
		<comments>http://talkbinary.com/programming/c/c-tail-recursion/#comments</comments>
		<pubDate>Thu, 14 May 2009 18:54:06 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[ML]]></category>
		<category><![CDATA[c++ factorial]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[factorial]]></category>
		<category><![CDATA[ml factorial]]></category>
		<category><![CDATA[ml recursion]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursion c++]]></category>
		<category><![CDATA[recursive function]]></category>
		<category><![CDATA[tail]]></category>
		<category><![CDATA[tail recursion]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=1647</guid>
		<description><![CDATA[Overview of Tail Recursion Tail Recursion is simply a special form of recursion, in which the recursive call is at the end of the function. This allows for improved efficiency as the final value to be computed is returned without any further calculation when the recursion function terminates. Factorial without Tail Recursion in C++ First<a class="moretag" href="http://talkbinary.com/programming/c/c-tail-recursion/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<blockquote><h3 id="section-1">Overview of Tail Recursion</h3>
<p>     Tail Recursion is simply a special form of recursion, in which the recursive call is at the end of the function. This allows for improved efficiency as the final value to be computed is returned without any further calculation when the recursion function terminates. </p></blockquote>
<h4 id="section-2">Factorial without Tail Recursion in C++</h4>
<p>     First a version without tail recursion will be shown in C++. This version requires that the function has to compute a new value to return to it&#8217;s caller after the function it called returned a value.</p>
<pre lang="c++">int fact(int n) {
     if ( n <= 1 ) return n;

     return n  * fact(n-1);
}</pre>
<p><br/></p>
<h4 id="section-3">Factorial with Tail Recursion in C++</h4>
<p>Below is the factorial function with tail recursion. Can you tell the difference from the one posted above? The function below doesn't need to do any more computations when the function it called returns (considering its not the last function that was called). It simply returns the value without any further calculation.</p>
<p>Usually, if it can be done using tail recursion, the function can be iteratively solved as well. </p>
<pre lang="c++">int fact(int n) {
     return fact_h(n, 1);
}

int fact_h(int n, int res) {
   if ( n <= 1 ) return res;

   return fact_h(n-1, n*res);
}</pre>
<h3 id="section-4">Tail Recursion in a functional language</h3>
<p>In C++, if you can write a tail recursive function most likely you can right an iterative version. In a functional language where iteration isn't possible, tail recursion allows for greater efficiency in such functions.</p>
<p><span id="more-1647"></span></p>
<h4 id="section-5">Recursive function in ML without tail recursion</h4>
<p>Below is a factorial function in <a href="http://talkbinary.com/programming/start-programming-in-ml/">ML</a>. This version does not implement tail recursion.</p>

<div class="wp_syntax"><div class="code"><pre class="sml" style="font-family:monospace;">fun fact 1  = 1 
| fact x = x * fact (x-1);</pre></div></div>

<p><br/></p>
<h4 id="section-6">Implementing Tail Recursion in ML</h4>
<p>The function below, does implement tail recursion. If you notice, this function does not require to do any computations after the function has returned as the function in the previous version.</p>

<div class="wp_syntax"><div class="code"><pre class="ml" style="font-family:monospace;">fun facth 1 n = n
| facth x n = facth (x-1) (n*x);
&nbsp;
fun fact 1 = 1
| fact x = facth x 1;</pre></div></div>

<h3 id="section-1">Overview of Tail Recursion</h3>
<p>So why is this faster? Well, first of all, it means you are uses the stack less, thus increasing efficiency. If you can implement tail recursion in a programming language such as C++, there must be an iterative version of the function. On the other hand if you are creating functions in a logic or functional programming language, try using tail recursion. On large projects you'll notice a great improvement in runtime. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-tail-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-05-23 08:47:17 -->
