{"id":1048,"date":"2014-11-08T00:01:19","date_gmt":"2014-11-08T08:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1048"},"modified":"2014-11-15T07:42:41","modified_gmt":"2014-11-15T15:42:41","slug":"functions-in-functions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1048","title":{"rendered":"Functions in Functions!"},"content":{"rendered":"<p>For an argument, functions in the C language can take literal values, equations, the result from another function, or any combination of these items. It&#8217;s a feature that&#8217;s both flexible and confusing.<br \/>\n<!--more--><br \/>\nAs an example, consider these three <em>printf()<\/em> functions:<\/p>\n<p><code>printf(\"The result is %d\\n\",27);<\/code><\/p>\n<p><code>printf(\"The result is %d\\n\",13+alpha);<\/code><\/p>\n<p><code>printf(\"The result is %d\\n\",blorf());<\/code><\/p>\n<p>The first statement uses a literal value. The second uses an equation, <code>13+alpha<\/code><\/code>, and the final uses the return value of the function <em>blorf()<\/em>, which you can assume generates an integer.<\/p>\n<p>It&#8217;s also possible to put the same function inside itself, as the following code demonstrates:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint twice(int v);\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    x = twice(twice(2));\r\n    printf(\"The result is %d\\n\",x);\r\n\r\n    return(0);\r\n}\r\n\r\nint twice(int v)\r\n{\r\n    return(v*2);\r\n}<\/pre>\n<p>At Line 9, the <em>twice()<\/em> function uses the result of the <em>twice()<\/em> function to calculate its value.<\/p>\n<p>Don&#8217;t freak out: No quantum particles were entangled to perform this type of operation. As with the sample <em>printf()<\/em> statements earlier, you simply need to work from inside the parentheses outward:<\/p>\n<p><code>x = twice(twice(2));<\/code><\/p>\n<p>The first operation is <code>twice(2)<\/code>, which simply doubles the value 2. The result 4 is returned.<\/p>\n<p>The result 4 is again passed to the <em>twice()<\/em> function, and the result 8 is returned.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>The result is 8<\/code><\/pre>\n<p>You could even get really carried away and do something like this for Line 9:<\/p>\n<p><code>x = twice(twice(twice(twice(2))));<\/code><\/p>\n<p>That all still works, providing you properly match up the parentheses.<\/p>\n<p>What you need to be careful of, however, is when you use a function <em>within<\/em> that same function. For example:<\/p>\n<pre class=\"screen\">\r\nint twice(int v)\r\n{\r\n    return(twice(v*2));\r\n}<\/pre>\n<p>In the <em>return<\/em> statement, the function <em>twice()<\/em> calls itself again. This is a perfectly legal maneuver in the C language. It&#8217;s called <em>recursion<\/em>.<\/p>\n<p><em>re-kur-shn<\/em><\/p>\n<p>The problem with the above example is that it&#8217;s a recursion without exit, so the function <em>twice()<\/em> would keep calling itself, doubling the value of <code>v<\/code>, until a nasty condition called a <em>stack overflow<\/em> occurs. That&#8217;s something you want to avoid.<\/p>\n<p>You can craft a function to be recursive. You&#8217;ll find many advantages to doing so, although be aware that it&#8217;s a tricky and advanced programming concept. I&#8217;ll gingerly explore the operation in next week&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1060\">Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A function can go inside a function, even when it&#8217;s the same function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1048\">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-1048","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\/1048","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=1048"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1048\/revisions"}],"predecessor-version":[{"id":1096,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1048\/revisions\/1096"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}