{"id":2189,"date":"2016-11-01T00:01:53","date_gmt":"2016-11-01T07:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2189"},"modified":"2016-11-11T07:32:37","modified_gmt":"2016-11-11T15:32:37","slug":"when-you-need-a-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2189","title":{"rendered":"When You Need a Function"},"content":{"rendered":"<p>I believe two reasons exist for creating functions. The first is repetition; when you have a chunk of code that duplicates several times, it&#8217;s best to shove it off into a function where it can be called repeatedly. The second reason is readability.<br \/>\n<!--more--><br \/>\nAs an example of readability, I recently wrote a program that featured several statements in the <em>main()<\/em> function that initialize various parts of the program. To make the code more readable, I cut all those statements and placed them into an <em>initialize()<\/em> function. That way the <em>main()<\/em> main function is rather brief, and that single statement &mdash; and its definition elsewhere in the code &mdash; is self-descriptive.<\/p>\n<p>For this month&#8217;s exercise, your task is to create one or more functions based on repetition as well as readability. Your job is to take the sample code, shown below, and create self-documenting functions that perform repetitive tasks:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char name[32], color[16], quest[64];\r\n\r\n    printf(\"What is your name? \");\r\n    scanf(\"%s\",name);\r\n    printf(\"What is your favorite color? \");\r\n    scanf(\"%s\",color);\r\n    printf(\"What is your quest? \");\r\n    scanf(\"%s\",quest);\r\n\r\n    printf(\"%s, whose favorite color is %s, \",\r\n            name,\r\n            color);\r\n    printf(\"you shall succeed!\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Yes, granted this is a short, silly program. Yet, as the program grows, functions become useful and necessary. When? That&#8217;s the point of the Exercise: Create one or more functions that reduce the length of the <em>main()<\/em> function.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2198\">Click here<\/a> to view my solution. Please try this exercise on your own before you peek at what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes functions are required, but they can add readability to your code. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2189\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2189","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2189","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=2189"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2189\/revisions"}],"predecessor-version":[{"id":2208,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2189\/revisions\/2208"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}