{"id":1068,"date":"2014-11-22T00:01:10","date_gmt":"2014-11-22T08:01:10","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1068"},"modified":"2014-11-29T07:58:41","modified_gmt":"2014-11-29T15:58:41","slug":"an-example-of-recursion","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1068","title":{"rendered":"An Example of Recursion"},"content":{"rendered":"<p>A recursive function calls itself. That&#8217;s a strange and beautiful concept, one that saves a lot of coding time, but how does it really work?<br \/>\n<!--more--><br \/>\nBefore I get into the traditional factorial recursion example, the following code demonstrates a simple and visual example of recursion.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint recurse(int v)\r\n{\r\n    printf(\"v = %d\\n\",v);\r\n    if( v == 5 )\r\n        return(v);\r\n    recurse(v+1);\r\n}\r\n\r\nint main()\r\n{\r\n    int a = 0;\r\n\r\n    recurse(a);\r\n\r\n    return(0);\r\n}<\/pre>\n<blockquote><p>If you build this code, you may see a <code>-Wreturn-type<\/code> compiler warning. The code is fine, despite the warning.<\/p><\/blockquote>\n<p>In this code, <em>recurse()<\/em> is a recursive function. It calls itself at Line 8:<\/p>\n<p><code>recurse(v+1);<\/code><\/p>\n<p>That statement is legal, and it works. No limitation exists on a function calling itself, but the code must have a way to escape. That escape valve is provided at Line 6 with an <em>if<\/em> statement. All the calls to <em>recurse()<\/em> at Line 8 are unwound by the <em>return<\/em> at Line 7.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>v = 0\r\nv = 1\r\nv = 2\r\nv = 3\r\nv = 4\r\nv = 5<\/code><\/pre>\n<p>So <code>v<\/code> keeps increasing because <em>recurse()<\/em> keeps calling itself. Then when <code>v<\/code> is equal to 5, the <em>return<\/em> statement kicks in and the whole thing unwinds. It&#8217;s a rather convoluted way to increment a variable 5 times, and nothing is really done with that variable, but the function is recursive.<\/p>\n<p>Now if it didn&#8217;t work, then the code would run amok. With <em>recurse()<\/em> continually calling itself, eventually the <em>stack pointer<\/em>, which keeps track of function calls in C, would exceed its allocated memory. To see that disaster in action, comment out Lines 6 and 7 in the code so that the <em>recurse()<\/em> function looks like this:<\/p>\n<pre class=\"screen\">\r\nint recurse(int v)\r\n{\r\n    printf(\"v = %d\\n\",v);\r\n<span class=\"comments\">\/\/  if( v == 5 )<\/span>\r\n<span class=\"comments\">\/\/      return(v);<\/span>\r\n    recurse(v+1);\r\n}<\/pre>\n<p>Build and run again.<\/p>\n<p>On my computer, I saw the following:<\/p>\n<pre><code>...\r\nv = 262000\r\nv = 262001\r\nv = 262002\r\nv = 262003\r\nv = 262004\r\nv = 262005\r\nSegmentation fault: 11<\/code><\/pre>\n<p>After calling itself 262,005 times, the <em>recurse()<\/em> function finally blew the stack. That&#8217;s the situation you want to avoid.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1080\">next week&#8217;s Lesson<\/a>, I&#8217;ll show how a factorial calculation can be done by using recursion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hopefully this simple demonstration doesn&#8217;t blow your mind. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1068\">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-1068","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\/1068","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=1068"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":1127,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1068\/revisions\/1127"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}