{"id":1080,"date":"2014-11-29T00:01:22","date_gmt":"2014-11-29T08:01:22","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1080"},"modified":"2014-11-22T07:51:18","modified_gmt":"2014-11-22T15:51:18","slug":"the-factorial-recursion","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1080","title":{"rendered":"The Factorial Recursion"},"content":{"rendered":"<p>It&#8217;s the example of recursion most often used, but it&#8217;s not the best example. Of course, to illustrate a better example you need to approach concepts such as fractals and graphical programming doodads. My favorite form of recursion is traversing a directory tree. Regardless, the code for a factorial recursion remains the go-to workhorse.<br \/>\n<!--more--><br \/>\nAs a reminder, a factorial is a mathematical calculation expressed as <em>n<\/em>!, which I pronounce, &#8220;N-bang.&#8221; Mathematicians would say, &#8220;N factorial,&#8221; but they&#8217;re not C programmers.<\/p>\n<p>The calculation for <em>n<\/em>! is <em>n<\/em> &times; <em>n<\/em>-1 &times; <em>n<\/em>-2 &times; <em>n<\/em>-3 . . . <em>n<\/em>&times;1.<\/p>\n<p>In an earlier <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1060\">Lesson<\/a>, I showed how this calculation could be coded by using a loop. Below you see an example of coding a factorial by using recursion, i.e., a function that calls itself.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nlong factorial(long f);\r\n\r\nint main()\r\n{\r\n    long a,b;\r\n\r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%ld\",&amp;a);\r\n    b = factorial(a);\r\n    printf(\"%ld! = %ld\\n\",a,b);\r\n\r\n    return(0);\r\n}\r\n\r\nlong factorial(long f)\r\n{\r\n    if(f == 1)\r\n        return(f);\r\n    else\r\n        return(f*factorial(f-1));\r\n}<\/pre>\n<p>The <em>factorial()<\/em> function calls itself at Line 22, which is also where the factorial calculation is made:<\/p>\n<pre><code>f*factorial(f-1);<\/code><\/pre>\n<p>The insane part of this process is that the function calls itself in the <em>return<\/em> statement, which is typical of many recursive functions.<\/p>\n<p>In the C language, what goes into the parentheses happens first, so before anything is returned, the function calls itself with a decremented value of variable <code>f<\/code>.<\/p>\n<p>The effect is that <code>f<\/code> slides from its initial value down to 1, which is caught at Line 19. Then the value 1 is returned at Line 20.<\/p>\n<p>Where is it returned to?<\/p>\n<p>The value is returned to Line 22, where it&#8217;s multiplied by the previous value of <code>f<\/code>, or <code>f<\/code>+1. So the recursive calculation happens as the function unwinds, and it&#8217;s done backwards: <code>f<\/code> &times; <code>f<\/code>+1 &times; <code>f<\/code>+2 &times; <code>f<\/code>+3 . . . &times; <code>f<\/code>. That value keeps returning until it&#8217;s finally returned from the function.<\/p>\n<p>That&#8217;s one recursive way to do a factorial. Other examples of recursion pop up in programming all the time, although it&#8217;s not as popular of a technique as working a loop.<\/p>\n<p>I admit that recursion isn&#8217;t the easiest thing to comprehend. In a future Lesson, I&#8217;ll explore traversing a directory tree by using recursion. That&#8217;s the example I coded that best taught me how recursion works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tried and true warhorse of the recursion examples. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1080\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1080","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1080"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions"}],"predecessor-version":[{"id":1109,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions\/1109"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}