{"id":1060,"date":"2014-11-15T00:01:50","date_gmt":"2014-11-15T08:01:50","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1060"},"modified":"2017-01-07T08:05:52","modified_gmt":"2017-01-07T16:05:52","slug":"introduction-to-recursion","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1060","title":{"rendered":"Introduction to Recursion"},"content":{"rendered":"<p>It&#8217;s truly a scary thing: A function calls itself. This trick can be done without damage to the space-time continuum, and nothing explodes when it&#8217;s done correctly.<br \/>\n<!--more--><br \/>\nIt&#8217;s a programming feature called <em>recursion<\/em>. Rather than avoid it, or just stand confused, you may discover that recursion comes in handy.<\/p>\n<p>As an example of recursion, I&#8217;ve been working on a text-mode version of the popular Minesweeper game, shown in Figure 1.<\/p>\n<div id=\"attachment_1063\" style=\"width: 470px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1063\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/11\/TerminalScreenSnapz001.png\" alt=\"Figure 1. My text-mode Minesweeper game.\" width=\"460\" height=\"359\" class=\"size-full wp-image-1063\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/11\/TerminalScreenSnapz001.png 460w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/11\/TerminalScreenSnapz001-300x234.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/11\/TerminalScreenSnapz001-384x300.png 384w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><p id=\"caption-attachment-1063\" class=\"wp-caption-text\">Figure 1. My text-mode Minesweeper game.<\/p><\/div>\n<p>As with the Windows version, when you &#8220;click&#8221; on an empty cell, the game reveals all other empty cells adjacent. It keeps doing that, until you have the large empty field, shown in Figure 1.<\/p>\n<p>The function that reveals the empty squares is recursive, i.e., it calls itself. That recursion saved a lot of redundant programming: If the cell adjacent is empty, then the same function is called again to hunt down more empty cells.<\/p>\n<p>As with all recursion, eventually an end condition appears. In my minesweeper game, a non-empty cell is found. At that point, all the recursive function calls &#8220;unwind,&#8221; and control is returned from the recursive function.<\/p>\n<p>Another example of recursion might be a function to traverse a maze: Every time an intersection is encountered, the function is called to turn in a certain direction. Eventually the maze is traversed, the end is reached, and the function unwinds. That&#8217;s a simple explanation.<\/p>\n<p>Recursion can often be easier to understand than it is to code. In a way, it&#8217;s like a loop in that recursion must feature an ending scenario. Without it, the recursion keeps winding tighter and tighter util the program crashes.<\/p>\n<p>The standard, &#8220;I&#8217;m completely not creative&#8221; programming example for recursion is to calculate a factorial value.<\/p>\n<p>Way back when you were sleeping through math, you missed the bit on factorials: It&#8217;s where you have an expression like 5!, which translates into 5&times;4&times;3&times;2&times;1. That&#8217;s the factorial of 5, which calculates to 120.<\/p>\n<p><em>Whatever<\/em>.<\/p>\n<p>In C, you could code a factorial by using a simple loop, as shown here:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    long a,f,x;\r\n\r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%ld\",&amp;f);\r\n    a = f;\r\n    for(x=f-1;x&gt;0;x--)\r\n        a *= x;\r\n    printf(\"%ld! = %ld\\n\",f,a);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code fetches input from the user, storing an integer value in variable <code>f<\/code>. A loop counts down from <code>f<\/code>-1 to 1. Each iteration, the value of variable <code>a<\/code> is multiplied by the countdown value. In effect, you get <code>f<\/code>! = <code>f<\/code> &times; (<code>f<\/code>-1) &times; (<code>f<\/code>-2) &times; (<code>f<\/code>-3) . . .&times; 1.<\/p>\n<p>Sample run:<\/p>\n<pre><code>Enter an integer: 18\r\n18! = 6402373705728000<\/code><\/pre>\n<p>The code works for small values, but when the numbers get bigger, the size of the <em>long int<\/em> variable overflows and the results are incorrect. But you get the point: The code calculates a factorial.<\/p>\n<p>To do the same thing with recursion, you need a factorial function. I&#8217;ll explore that function in a future Lesson. For <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1068\">next week&#8217;s Lesson<\/a>, I&#8217;ll show a simple and hopefully clear example of recursion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The mind-blowing concept of a function calling itself. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1060\">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-1060","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\/1060","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=1060"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":2301,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions\/2301"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}