<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C++ Tutorial - Functions</title><link>/technologies/code_exchange/b/blog/posts/c-tutorial---functions</link><description>IntroductionIn the previous tutorial , we learned how to make our code more robust by using while loops to screen out bad user input. In this tutorial, we are going to learn how to make our code more modular and reusable by utilizing functions.&amp;amp;...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: C++ Tutorial - Functions</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-tutorial---functions</link><pubDate>Mon, 11 Feb 2013 00:21:26 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:1ab21683-0241-4393-889b-f96bedfb6e6b</guid><dc:creator>oneleggedredcow</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;So, it dawned on me after I wrote the article, that a better implementation of the combination fuction would look something like this:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; Combination(&lt;/span&gt;&lt;span&gt;int&lt;/span&gt; &lt;span&gt;n&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;int&lt;/span&gt; &lt;span&gt;r&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;span&gt;int&lt;/span&gt;&lt;/span&gt;&lt;span&gt; ret = 1;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;for&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; i = &lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;; i &amp;gt; &lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt; - &lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span&gt;; i--)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = ret * i;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt; &lt;/span&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;return&lt;/span&gt;&lt;span&gt; ret / Factorial(&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;This is an improvement, because it will handle much larger values of n.&amp;nbsp; So, say you wanted to compute the number of 5 card hands that could be made with a standard deck of cards.&amp;nbsp; The answer would be Combination(52, 5).&amp;nbsp; The problem is that 52! is a very large number.&amp;nbsp; Much larger than what can be stored in an integer, as we will go over in the next tutorial.&amp;nbsp; So, when you go to compute this, you won&amp;#39;t get what you expect. This new implementation helps by computing 52!/47! much how you would by hand.&amp;nbsp; Namely that it is 52*51*50*49*48*47!/47!, and the 47! cancels out and leaves us 52*51*50*49*48.&amp;nbsp; This means that you never have to compute 52! or 47!, which saves us from those very big numbers.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Unfortunately, it doesn&amp;#39;t show the use of functions as nicely, so maybe it is better that I didn&amp;#39;t think of it after all!&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=14959&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>