{"id":637,"date":"2014-04-26T00:01:11","date_gmt":"2014-04-26T07:01:11","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=637"},"modified":"2014-04-19T07:44:06","modified_gmt":"2014-04-19T14:44:06","slug":"hello-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=637","title":{"rendered":"Hello, Function!"},"content":{"rendered":"<p>A basic C language process is passing a value to a function. If you&#8217;re an old hand, you might say, &#8220;Yeah, well, so?&#8221; For a beginning programmer, however, functions and their arguments can be a confusing and intimidating ordeal.<br \/>\n<!--more--><br \/>\nA function in C can:<\/p>\n<ul>\n<li>Accept a value<\/li>\n<li>Return a value<\/li>\n<li>A combination of both or neither<\/li>\n<\/ul>\n<p>A function that neither accepts nor returns a value is a <em>void<\/em> function. I call it a function that doesn&#8217;t <em>funct<\/em>. That label doesn&#8217;t imply that the function is unnecessary or that your programming skills are weak when you create a <em>void<\/em> function. In fact, it doesn&#8217;t mean anything. I frequently use <em>void<\/em> functions to store repetitious code in my programs. Here&#8217;s an example of such a function:<\/p>\n<pre class=\"screen\">\r\nvoid pany(void)\r\n{\r\n    printf(\"Press Enter to continue:\");\r\n    getchar();\r\n}<\/pre>\n<p>The <em>pany()<\/em> function displays a prompt and waits for the Enter key to be pressed. It requires no arguments and returns no values, which is why the word <em>void<\/em> is used in its declaration.<\/p>\n<p>Values passed to a function are referred to as <em>arguments<\/em>. Functions can accept any quantity of arguments, from zero (<em>void<\/em>) to whatever is practical. The quantity depends on what the function does. All arguments must be declared as a specific variable type. The arguments are also given names for reference within the function. For example:<\/p>\n<p><code>void guess(int tries)<\/code><\/p>\n<p>The <em>guess()<\/em> function has a single argument, an <em>int<\/em> value. The variable <code>tries<\/code> is used within the function to represent the <em>int<\/em> value passed to the function. For example, elsewhere in the code you may see:<\/p>\n<p><code>guess(3);<\/code><\/p>\n<p>The value 3 is &#8220;passed&#8221; to the <em>guess()<\/em> function. Then, within that function, the value is referenced by variable <code>tries<\/code>.<\/p>\n<p>What can be confusing, especially for a beginner, is that the function could also be called by using a variable. For example:<\/p>\n<p><code>guess(attempt);<\/code><\/p>\n<p>Above, the <em>int<\/em> variable <code>attempt<\/code> is used to call the <em>guess()<\/em> function. The value of <code>attempt<\/code> is passed to the function &#8212; the value only, not the variable name. Inside the function, the value is referenced by using the <code>tries<\/code> variable.<\/p>\n<p>Remember from my C programming books, variables are <em>local<\/em> to their functions. Unless <code>attempt<\/code> is a global variable, you can&#8217;t use that variable within another function.<\/p>\n<p>A function in the C language returns only one value. That value is specified as the function&#8217;s variable type. For example:<\/p>\n<p><code>int whatever(void);<\/code><\/p>\n<p>Above, the <em>whatever()<\/em> function returns an <em>int<\/em> value. The function has no arguments.<\/p>\n<p><code>char middle(char *text);<\/code><\/p>\n<p>Above, the <em>middle()<\/em> function returns a <em>char<\/em> value. The function accepts a single string as an argument, a <em>char<\/em> pointer.<\/p>\n<p>Functions in C cannot return more than a single value. When you absolutely need such a function (and I can&#8217;t think of any examples off the top of my head) you must use global variables.<\/p>\n<p>What about functions that return a structure variable? When that happens, the structure must be defined as a global variable, otherwise it can&#8217;t be used outside of the function. Beyond that, functions in the C language return only a single value, or no value at all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a basic review, or some pointers if you&#8217;re just starting, on how a function&#8217;s arguments work. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=637\">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":[2],"tags":[],"class_list":["post-637","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\/637","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=637"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":679,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions\/679"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}