{"id":199,"date":"2022-01-20T10:14:45","date_gmt":"2022-01-20T10:14:45","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=199"},"modified":"2022-01-20T10:14:45","modified_gmt":"2022-01-20T10:14:45","slug":"scala-functions","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-functions\/","title":{"rendered":"Scala &#8211; Functions"},"content":{"rendered":"<p>In this lesson, you will learn how functions work and how to write and call functions in Scala.<\/p>\n<p>A function in Scala is a collection of statements that perform some given operation. Scala permits nested functions which means that a function can be defined inside another function. Moreover, function names in Scala can contain characters like +, &amp;, -, \/ etc.<\/p>\n<p>The following is covered here:<\/p>\n<ol>\n<li><a href=\"#t1\">Function Declaration and Definition<\/a><\/li>\n<li><a href=\"#t2\">Calling a Function<\/a><\/li>\n<li><a href=\"#t3\">Nested Functions<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Function Declaration and Definition<\/strong><\/h4>\n<p><strong>Function declaration<\/strong> specifies the name of the function and the return type. The syntax for declaring a function in Scala is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> functionName <span style=\"color: #333333;\">([<\/span><span style=\"color: #333399; font-weight: bold;\">parameters<\/span> <span style=\"color: #333399; font-weight: bold;\">list<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">type<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">]<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Function definition specifies everything about the function. It includes the function declaration and the function body.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> functionName <span style=\"color: #333333;\">([<\/span><span style=\"color: #333399; font-weight: bold;\">parameters<\/span> <span style=\"color: #333399; font-weight: bold;\">list<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">type<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   statements\r\n   <span style=\"color: #333333;\">....<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">expr<\/span><span style=\"color: #333333;\">]<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Note that the &#8220;expr&#8221; in the return statement is optional. This means that a function may or may not return a value. Also note that the parameter list is optional as well.<\/p>\n<p>The code below is an example of a function that takes two parameters which are numbers and returns the product of the two numbers:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> getProduct <span style=\"color: #333333;\">(<\/span>num1<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> num2<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> product<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\n  product <span style=\"color: #008800; font-weight: bold;\">=<\/span> num1 <span style=\"color: #333333;\">*<\/span> num2\r\n  <span style=\"color: #008800; font-weight: bold;\">return<\/span>  product\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Unit Functions<\/strong> &#8211; These are also called Units. They are functions that does not return anything similar to void functions in Java.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Calling a Function<\/strong><\/h4>\n<p>Function call in Scala is similar to function call in Java. You simply specify the function name and the arguments to the function. Let&#8217;s write a program that includes a function that calculates the max of three numbers. They we call this function using three values.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ScalaTutorial<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> findMax <span style=\"color: #333333;\">(<\/span>num1<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> num2<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> num3<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> max<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> num1\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>num2 <span style=\"color: #333333;\">&gt;<\/span> num1<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      max <span style=\"color: #008800; font-weight: bold;\">=<\/span> num2\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>num3 <span style=\"color: #333333;\">&gt;<\/span> num1<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      max <span style=\"color: #008800; font-weight: bold;\">=<\/span> num3\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span>  max\r\n  <span style=\"color: #333333;\">}<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"The max of 5, 12, 9 is \"<\/span> <span style=\"color: #333333;\">+<\/span> findMax<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">12<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">9<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">)<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Here we write a function called <em><strong>findMax()<\/strong><\/em>. This function using Conditional Statement to find the max of three numbers. Then in the main function, we call the findMax() function with three values and the max of the three numbers is returned.<\/p>\n<p>Note that you can also use the max() function available in the scala.max package to achieve the same thing.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Nested Functions<\/strong><\/h4>\n<p>In Scala, you can actually define a function inside another function. This is called a nested function. The syntax for nested function is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> function1<span style=\"color: #333333;\">()<\/span>    <span style=\"color: #888888;\">\/\/Outer function<\/span>\r\n     <span style=\"color: #008800; font-weight: bold;\">def<\/span> function2<span style=\"color: #333333;\">(){<\/span>\r\n\t <span style=\"color: #888888;\">\/\/nested function body<\/span>\r\n     <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>  \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The program below is an example of nested functions to find the factorial of\u00a0 numbers.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">NestedFunctionDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">def<\/span> factorial<span style=\"color: #333333;\">(<\/span>i<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{\r\n<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">def<\/span> fact<span style=\"color: #333333;\">(<\/span>i<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> accumulator<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span> <span style=\"color: #888888;\">\/\/Nested function<\/span>\r\n         <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>i <span style=\"color: #333333;\">&lt;=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span>\r\n            accumulator\r\n         <span style=\"color: #008800; font-weight: bold;\">else<\/span>\r\n            fact<span style=\"color: #333333;\">(<\/span>i <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> i <span style=\"color: #333333;\">*<\/span> accumulator<span style=\"color: #333333;\">)<\/span>\r\n      <span style=\"color: #333333;\">}\r\n<\/span>\r\n      fact<span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span> factorial<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">)<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span> factorial<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">)<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span> factorial<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">)<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>In the code above, we have a function <em><strong>fact()<\/strong><\/em> nested inside a parent function <em><strong>factorial()<\/strong><\/em>. The fact() takes a number and some total (accumulator) and multiplies accumulator by the given number minus 1. However, if the given number is &lt;= 1. Total is returned, meaning that the factorial has been found. The the factorial() function takes a number\u00a0 and finds the factorial of the number by repeatedly calling the fact() function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, you will learn how functions work and how to write and call functions in Scala. A function in Scala is a collection &hellip; <\/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":[33],"class_list":["post-199","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-scala-functions"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=199"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/199\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/199\/revisions\/207"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}