<?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; recursion c++</title>
	<atom:link href="http://talkbinary.com/tag/recursion-c/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>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>
		<item>
		<title>C++ Helper Functions</title>
		<link>http://talkbinary.com/programming/c/c-helper-functions/</link>
		<comments>http://talkbinary.com/programming/c/c-helper-functions/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 07:03:24 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[helper functions]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[recursion c++]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=1691</guid>
		<description><![CDATA[Overview A helper function is merely a function that passes additional arguments to another function of a similar name. The additional arguments that are passed in are usually predetermined by the programmer that allow the use of recursion. This tutorial considers you are knowledgeable with recursion since our example deals with a recursive function. Example<a class="moretag" href="http://talkbinary.com/programming/c/c-helper-functions/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">Overview</h3>
<p>A helper function is merely a function that passes additional arguments to another function of a similar name. The additional arguments that are passed in are usually predetermined by the programmer that allow the use of recursion. </p>
<p>This tutorial considers you are knowledgeable with <a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">recursion</a> since our example deals with a recursive function.</p>
<h3 id="section-2">Example of a helper function</h3>
<p>Below we&#8217;ll provide you an example of when you might encounter the use of a helper function. We&#8217;ll start off by introducing a simple print function that prints all the elements of a vector. </p>
<pre lang="cpp" line=1>void print(vector <int> vec) { 

     for ( int i = 0; i < vec.size(); i++ ) {
          cout << i << ": " << vec[i] << endl;
     }
}</pre>
<p><br/><br />
This function would be called in your main program in the following way.</p>
<pre lang="cpp" line=1>
#include <iostream>
#include <vector>

using namespace std;

int main() { 

     vector <int> vec;

     /* Fill the vector with values...using push_back() perhaps? */

     //Calling of our function
     print(vec);

     return 0;
}</pre>
<p><br/></p>
<h4 id="section-3">Where the helper function comes in</h4>
<p>Now, imagine for some reason you want to try making your print function recursively. The thing is, if you want to print using recursion, you need your function to have an additional parameter in order to track what element we are currently printing.<br />
<span id="more-1691"></span><br />
This would mean that we would have to change all our calls to our print function to include an additional parameter.  Well, we don't if we use a helper function. By simply having our original print function call our helper function, we avoid changing the names of all our calls.</p>
<pre lang="cpp" line=1>void print(vector <int> vec) { 

     print_h(vec, 0);

}

void print_h(vector <int> vec, int i) { 

    if ( i > vec.size() ) {
        return;
    }

    cout << i << ": " << vec[i] << endl;

    print_h(vec, i+1);

    return;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-helper-functions/feed/</wfw:commentRss>
		<slash:comments>4</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 20:10:26 -->
