{"id":2175,"date":"2013-04-05T16:38:23","date_gmt":"2013-04-05T23:38:23","guid":{"rendered":"http:\/\/jeffreyatw.com\/blog\/?p=2175"},"modified":"2013-04-05T16:38:23","modified_gmt":"2013-04-05T23:38:23","slug":"what-is-a-function","status":"publish","type":"post","link":"https:\/\/jeffreyatw.com\/blog\/2013\/04\/what-is-a-function\/","title":{"rendered":"What is a function?"},"content":{"rendered":"<p><em>I&#8217;m trying my hand at writing a ridiculously easy-to-follow guide on JavaScript essentials. There might be many of these out there, but this one is mine. Tell me what you think and whether I can clarify or simplify at any point.<\/em><br \/>\nIn JavaScript, a <dfn>function<\/dfn> is a bunch of code that can be run, or called, repeatedly.<\/p>\n<h2>Creating a function<\/h2>\n<p>Here is an example of a function being created:<br \/>\n[javascript]<br \/>\nvar doStuff = function () {<br \/>\n  \/\/ Code you want to run goes here!<br \/>\n}<br \/>\n[\/javascript]<br \/>\nIn this example, we are first creating a variable called <code>doStuff<\/code>. <code>doStuff<\/code> is then being assigned a function as its value &#8211; which means, from now on when we refer to <code>doStuff<\/code>, we are referring to a bunch of code that can be called.<\/p>\n<h2>Calling a function<\/h2>\n<p>Whenever you see parentheses following a variable, it means we are <dfn>calling<\/dfn> a function &#8211; the code inside it will be run:<br \/>\n[javascript]<br \/>\ndoStuff(); \/\/ the doStuff function is being called.<br \/>\n[\/javascript]<br \/>\nThis is in contrast to just referring to a variable, which is valid, but doesn&#8217;t do anything:<br \/>\n[javascript]<br \/>\ndoStuff; \/\/ no-op<br \/>\n[\/javascript]<\/p>\n<h2>Arguments<\/h2>\n<p>Some functions can change what they do, based on how they are called.<br \/>\nWhen we are writing code for the browser, we automatically have access to a function named <code>alert<\/code>, which tells the web browser to pop up a dialog box. When we call <code>alert<\/code>, we need to specify what exactly to show inside that dialog box:<br \/>\n[javascript]<br \/>\n\/\/ the alert function is being called with the string &#8216;Hello!&#8217;<br \/>\nalert(&#8216;Hello!&#8217;);<br \/>\n[\/javascript]<br \/>\nIn this example, we are putting a string inside the parentheses. Anything placed inside these parentheses is called an <dfn>argument<\/dfn>. In the case of <code>alert<\/code>, the argument determines what text appears in the dialog box that shows up:<br \/>\n<a href=\"http:\/\/138.68.233.146\/blog\/wp-content\/uploads\/2013\/04\/Screen-Shot-2013-04-05-at-5.19.18-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2183\" alt=\"Screen Shot 2013-04-05 at 5.19.18 PM\" src=\"http:\/\/138.68.233.146\/blog\/wp-content\/uploads\/2013\/04\/Screen-Shot-2013-04-05-at-5.19.18-PM.png\" width=\"534\" height=\"267\" srcset=\"https:\/\/jeffreyatw.com\/blog\/wp-content\/uploads\/2013\/04\/Screen-Shot-2013-04-05-at-5.19.18-PM.png 534w, https:\/\/jeffreyatw.com\/blog\/wp-content\/uploads\/2013\/04\/Screen-Shot-2013-04-05-at-5.19.18-PM-300x150.png 300w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/a><br \/>\nIf we want to create a function that makes use of an argument, it would look like this:<br \/>\n[javascript]<br \/>\nvar takeThis = function (foo) {<br \/>\n  \/\/ Inside these brackets,<br \/>\n  \/\/ we have access to a variable named foo.<br \/>\n}<br \/>\n\/\/ foo&#8217;s value will be &#8220;Hello!&#8221; when the function is called.<br \/>\ntakeThis(&#8220;Hello!&#8221;);<br \/>\n\/\/ foo&#8217;s value will be 3 when the function is called.<br \/>\ntakeThis(3);<br \/>\n[\/javascript]<br \/>\nIn this example, we are saying that whatever is passed in as an argument when we call <code>takeThis<\/code> will be referred to as <code>foo<\/code> as the function runs. We can name the argument whatever we want, but it only affects how it will be referred to inside the function&#8217;s code.<\/p>\n<h2>Returning<\/h2>\n<p>Functions can also give back a result after they&#8217;ve been called. This is called <dfn>returning<\/dfn>. Returning is important if we want to receive the result of a function and use it someplace else:<br \/>\n[javascript]<br \/>\nvar giveMeSomething = function () {<br \/>\n  return &#8220;Here you go&#8221;;<br \/>\n}<br \/>\nvar something = giveMeSomething();<br \/>\n\/\/ now something&#8217;s value will be &#8220;Here you go&#8221;<br \/>\n[\/javascript]<br \/>\nIn this example, we are using the command <code>return<\/code> to specify what comes out of the function when it is called. Upon calling <code>giveMeSomething<\/code>, we are taking its result and assigning it as the value of a new variable named <code>something<\/code>.<\/p>\n<h2>Multiple arguments<\/h2>\n<p>Functions can accept multiple arguments. It is as simple as separating the values being passed in with a comma. In this example, we are creating a function that can add two numbers together and return the result:<br \/>\n[javascript]<br \/>\nvar add = function (addend1, addend2) {<br \/>\n  \/\/ add the two arguments together and then return the result<br \/>\n  return addend1 + addend2;<br \/>\n}<br \/>\n\/\/ pass 1 and 2 when calling add and assign its result, 3, to sum<br \/>\nvar sum = add(1, 2);<br \/>\n[\/javascript]<br \/>\nHaving called <code>add<\/code> with <code>1<\/code> and <code>2<\/code> as the arguments, those values are then assigned to <code>addend1<\/code> and <code>addend2<\/code>, respectively.<\/p>\n<h2>Calling functions inside other functions<\/h2>\n<p>There&#8217;s nothing stopping us from calling functions within the code of another:<br \/>\n[javascript]<br \/>\nvar sayHello = function () {<br \/>\n  alert(&#8220;Hello!&#8221;);<br \/>\n}<br \/>\n[\/javascript]<br \/>\nHere, we&#8217;re creating our own function and calling it inside another function we&#8217;re creating:<br \/>\n[javascript]<br \/>\nvar bar = function () {<br \/>\n  \/\/ do something<br \/>\n}<br \/>\nvar foo = function () {<br \/>\n  bar();<br \/>\n}<br \/>\nfoo(); \/\/ call foo, which will call bar<br \/>\n[\/javascript]<br \/>\nHere&#8217;s a more complex example where we&#8217;re passing in arguments and getting things returned to us:<\/p>\n<div class=\"function_steps_code_1\">\n[javascript]<br \/>\nvar bar = function (arg) {<br \/>\n  return arg &#8211; 1<br \/>\n}<br \/>\nvar foo = function (arg) {<br \/>\n  return bar(arg) &#8211; 1;<br \/>\n}<br \/>\nvar result = foo(3);<br \/>\n[\/javascript]\n<\/div>\n<p>Seem complicated? Hover over these steps to see how this code will run:<\/p>\n<ol data-code=\"function_steps_code_1\">\n<li data-line=\"1\"><code>bar<\/code> is defined and assigned<\/li>\n<li data-line=\"5\"><code>foo<\/code> is defined and assigned<\/li>\n<li data-line=\"9\"><code>foo<\/code> is called with <code>3<\/code> passed as the argument<\/li>\n<li data-line=\"5\"><code>foo<\/code> begins to run, with <code>arg<\/code> having a value of <code>3<\/code><\/li>\n<li data-line=\"6\"><code>bar<\/code> is called with <code>arg<\/code> passed as the argument<\/li>\n<li data-line=\"1\"><code>bar<\/code> begins to run, with its own version of <code>arg<\/code> having a value of <code>3<\/code><\/li>\n<li data-line=\"2\"><code>arg<\/code> is subtracted by <code>1<\/code>, and returned<\/li>\n<li data-line=\"6\">The result of <code>bar(arg)<\/code>, which is <code>2<\/code>, is subtracted by <code>1<\/code>, and returned<\/li>\n<li data-line=\"9\">The result of <code>foo(3)<\/code>, which is <code>1<\/code>, is assigned to <code>result<\/code>.<\/li>\n<\/ol>\n<h2>Passing functions into other functions<\/h2>\n<p>To take it one step further, we can even pass functions themselves as arguments:<\/p>\n<div class=\"function_steps_code_2\">\n[javascript]<br \/>\nvar bar = function () {<br \/>\n  \/\/ do something<br \/>\n}<br \/>\nvar foo = function (callMe) {<br \/>\n  \/\/ we are assuming that the argument is a function<br \/>\n  callMe();<br \/>\n}<br \/>\n\/\/ we are not calling bar here,<br \/>\n\/\/ merely passing it in as an argument while calling foo<br \/>\nfoo(bar);<br \/>\n[\/javascript]\n<\/div>\n<p>This last example might take a bit of time to understand. The difference between merely referring to a function versus calling it is an important distinction.<\/p>\n<ol data-code=\"function_steps_code_2\">\n<li data-line=\"12\">When we run <code>foo(bar);<\/code> we are calling <code>foo<\/code>, but not calling <code>bar<\/code> &#8211; remember, we don&#8217;t see a pair of parentheses following it &#8211; we&#8217;re only referring to it and passing it in.<\/li>\n<li data-line=\"5\">When foo begins to run, it has access to a variable named <code>callMe<\/code>. Having specified the function <code>bar<\/code> as the argument, <code>callMe<\/code> now refers to it as well.<\/li>\n<li data-line=\"7\"><code>callMe<\/code>, or <code>bar<\/code>, is called.<\/li>\n<\/ol>\n<p>Why would we want to do this? So we can call <code>foo<\/code> multiple times and tell it specifically which function to run each time. In the real world, <code>foo<\/code> would do more than just call another function &#8211; it might do something with the result of that function, or return something itself.<\/p>\n<hr \/>\n<p>That&#8217;s it for an introduction. I&#8217;ll see if I can write something about more complex topics like scopes, <code>this<\/code>, recursion, closures, asynchronicity, et cetera &#8211; but we&#8217;ll start here and see how it goes.<br \/>\n<script type=\"text\/javascript\">\n<!--\n$(function () {\n  var $functionSteps = $('[data-code]');\n  $functionSteps.on('mouseenter', 'li', function (e) {\n    $('.' + $(e.delegateTarget).data('code')).find('.number' + $(this).data('line')).addClass('highlighted');\n  });\n  $functionSteps.on('mouseleave', 'li', function (e) {\n    $('.' + $(e.delegateTarget).data('code')).find('.line').removeClass('highlighted');\n  });\n});\n\/\/--><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying my hand at writing a ridiculously easy-to-follow guide on JavaScript essentials. There might be many of these out there, but this one is mine. Tell me what you think and whether I can clarify or simplify at any point. In JavaScript, a function is a bunch of code that can be run, or&hellip; <a class=\"more-link\" href=\"https:\/\/jeffreyatw.com\/blog\/2013\/04\/what-is-a-function\/\">Continue reading <span class=\"screen-reader-text\">What is a function?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[38,57,80,101,108],"class_list":["post-2175","post","type-post","status-publish","format-standard","hentry","category-blog","tag-functions","tag-javascript","tag-programming","tag-teaching","tag-tutorial","entry"],"_links":{"self":[{"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/posts\/2175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/comments?post=2175"}],"version-history":[{"count":0,"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/posts\/2175\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/media?parent=2175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/categories?post=2175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeffreyatw.com\/blog\/wp-json\/wp\/v2\/tags?post=2175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}