{"id":9208,"date":"2022-03-16T19:58:11","date_gmt":"2022-03-16T19:58:11","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9208"},"modified":"2022-03-16T19:58:11","modified_gmt":"2022-03-16T19:58:11","slug":"erlang-modules-and-functions","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/erlang-modules-and-functions\/","title":{"rendered":"Erlang modules and functions"},"content":{"rendered":"<p><strong>Modules<\/strong><\/p>\n<p>Modules are (what you might expect) a way to group together functions.<\/p>\n<p>We declare a module with the first line<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n-module(Name).\r\n<\/pre>\n<p>This is always the first line of a module and the Name is an atom (an atom being a literal\/constant).<\/p>\n<p>Next up in our module, we&#8217;ll be exporting functions along with their arity (arity being an integer representing the number of arguments that can be passed to the function). So for example, if we have <em>add<\/em> with two arguments we would export it like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n-export(&#x5B;add\/2]).\r\n<\/pre>\n<p>We can export multiple functions within the <em>-export<\/em> for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n-export(&#x5B;add\/2, delete\/2, multiply\/2, divide\/2]).\r\n<\/pre>\n<p>So let&#8217;s put those pieces together and we get a <em>math<\/em> module something like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n-module(math).\r\n-export(&#x5B;add\/2, subtract\/2, multiply\/2, divide\/2]).\r\n\r\nadd(A, B) -&gt; \r\n    A + B.\r\n\r\nsubtract(A, B) -&gt; \r\n    A - B.\r\n\r\nmultiply(A, B) -&gt; \r\n    A * B.\r\n\r\ndivide(A, B) -&gt; \r\n    A \/ B.\r\n<\/pre>\n<p><strong>Functions<\/strong><\/p>\n<p>So we&#8217;ve seen some example of functions, let&#8217;s look at the syntax.<\/p>\n<p>Functions take the form <em>Name(Args) -> Body.<\/em> where each expression in the body ends with a comma, so for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nstart() -&gt; \r\n    S = &quot;Hello World&quot;,\r\n    io:fwrite(S).\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Modules Modules are (what you might expect) a way to group together functions. We declare a module with the first line -module(Name). This is always the first line of a module and the Name is an atom (an atom being a literal\/constant). Next up in our module, we&#8217;ll be exporting functions along with their arity [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[325],"tags":[],"class_list":["post-9208","post","type-post","status-publish","format-standard","hentry","category-erlang"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9208"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9208\/revisions"}],"predecessor-version":[{"id":9209,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9208\/revisions\/9209"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}