{"id":208,"date":"2022-01-20T13:09:04","date_gmt":"2022-01-20T13:09:04","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=208"},"modified":"2022-01-20T13:09:04","modified_gmt":"2022-01-20T13:09:04","slug":"scala-more-on-functions","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-more-on-functions\/","title":{"rendered":"Scala &#8211; More on Functions"},"content":{"rendered":"<p>In this lesson, we would be covering function call by-value parameters.<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Function Call by Name<\/a><\/li>\n<li><a href=\"#t2\">Functions With Variable Parameters<\/a><\/li>\n<li><a href=\"#t3\">Functions With Default Arguments<\/a><\/li>\n<li><a href=\"#t4\">Functions With Named Arguments<\/a><\/li>\n<li><a href=\"#t5\">Recursive Functions<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Function Call by Name<\/strong><\/h4>\n<p>Call-by-name allows us to pass the name of a function as parameter to another function call. This means that the function parameter is evaluated each time the calling function is executed.<\/p>\n<p>In the example below, we have a function getTime that returns the current system time. Then we have another function currentTime that takes a time object as parameter.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demostrate Call by Name<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">CallByNameDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> getTime<span style=\"color: #333333;\">()<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Getting system time...\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #bb0066; font-weight: bold;\">System<\/span><span style=\"color: #333333;\">.<\/span>nanoTime<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> currentTime<span style=\"color: #333333;\">(<\/span>t<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333333;\">=&gt;<\/span><span style=\"color: #bb0066; font-weight: bold;\">Long<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">={<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Now in currentTime function...\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Time parameter is \"<\/span> <span style=\"color: #333333;\">+<\/span> t<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    currentTime<span style=\"color: #333333;\">(<\/span>getTime<span style=\"color: #333333;\">())<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Please try it out and see the output.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Now in currentTime function...\r\nGetting system time...\r\nTime parameter is 370723546094499\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Function With Variable Parameters<\/strong><\/h4>\n<p>In this kind of functions, you specify a parameter and then indicate that this parameter could be repeated number of time. For example, if a function takes 5 integer parameters, you can indicate this using Int*. An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demostrate Repeated parameters<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FunctionDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> printNums<span style=\"color: #333333;\">(<\/span>nums<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: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/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: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>num <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> nums<span style=\"color: #333333;\">){<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Num [\"<\/span> <span style=\"color: #333333;\">+<\/span> i <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"] = \"<\/span> <span style=\"color: #333333;\">+<\/span> num <span style=\"color: #333333;\">)<\/span>\r\n      i <span style=\"color: #008800; font-weight: bold;\">=<\/span> i <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n    <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    printNums<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">34<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">65<\/span><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;\">37<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">76<\/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>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Function Default Argument<\/strong><\/h4>\n<p>In Scala, you can define a function and specify default arguments for the parameter. This means that when the function is called without any argument, it would evaluate using the default values specified in the function definition. An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demonstrate Default argument<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">DefaultArgumentDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> getSum<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> <span style=\"color: #0000dd; font-weight: bold;\">10<\/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: #0000dd; font-weight: bold;\">20<\/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> sum<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><span style=\"color: #333333;\">;<\/span>\r\n    sum <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> sum <span style=\"color: #888888;\">\/\/return keyword is redundant but ok<\/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><span style=\"background-color: #fff0f0;\">\"The sum is \"<\/span> <span style=\"color: #333333;\">+<\/span> getSum<span style=\"color: #333333;\">())<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>So even though no argument is specified in the\u00a0 function call, the sum is calculated as 30 using default arguments.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Function with Named Arguments<\/strong><\/h4>\n<p>Normally, in a function call, you pass arguments to the function in the order they appear in the function definition. However, you can also pass the arguments to the functions in different order using named arguments. The program below uses named arguments for the function call<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demostrate Named argument<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">NamedArgumentDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> check<span style=\"color: #333333;\">(<\/span>a<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> b<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> c<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;\">Unit<\/span> <span style=\"color: #333333;\">=<\/span>  <span style=\"color: #333333;\">{<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Argument 1 is \"<\/span> <span style=\"color: #333333;\">+<\/span> a<span style=\"color: #333333;\">)<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Argument 2 is \"<\/span> <span style=\"color: #333333;\">+<\/span> b<span style=\"color: #333333;\">)<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Argument 3 is \"<\/span> <span style=\"color: #333333;\">+<\/span> c<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    check<span style=\"color: #333333;\">(<\/span>b <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">40<\/span><span style=\"color: #333333;\">,<\/span> c <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">12<\/span><span style=\"color: #333333;\">,<\/span> a <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">9<\/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>If you execute this program, you will see that the output is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Argument 1 is 9\r\nArgument 2 is 40\r\nArgument 3 is 12\r\n<\/pre>\n<p>So even though the arguments were passed out of order, we still have them ordered using named arguments<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Recursive Functions<\/strong><\/h4>\n<p>Recursion is a very important concept in programming and Scala is not an exception. Let&#8217;s write a Scala program to perform factorial using recursion. As you know, a recursive function is a function that calls itself.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demostrate Recursive Function<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">RecursionDemo<\/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>num<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> fact<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;\">1<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>num <span style=\"color: #333333;\">&lt;=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      fact <span style=\"color: #008800; font-weight: bold;\">=<\/span>  <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      fact <span style=\"color: #008800; font-weight: bold;\">=<\/span> num <span style=\"color: #333333;\">*<\/span> factorial<span style=\"color: #333333;\">(<\/span>num <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    <span style=\"color: #008800; font-weight: bold;\">return<\/span> fact\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;\">\"Factorial of 5 is \"<\/span> <span style=\"color: #333333;\">+<\/span> factorial<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/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>&nbsp;<\/p>\n<p>I recommend you ensure to enter these programs and run them to see the output. Also feel free to watch the free video tutorials for a clearer step by step explanation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would be covering function call by-value parameters. We would cover the following: Function Call by Name Functions With Variable Parameters Functions &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":[34],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-functions"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/208","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=208"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/208\/revisions\/211"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}