{"id":215,"date":"2022-01-21T10:07:06","date_gmt":"2022-01-21T10:07:06","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=215"},"modified":"2022-01-21T10:07:06","modified_gmt":"2022-01-21T10:07:06","slug":"scala-closure","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/","title":{"rendered":"Scala &#8211; Closure"},"content":{"rendered":"<p>A closure in Scala is a function and the concept of closure is quite clear. However I have decided to discuss closure under its own lesson since it is a concept unique to Scala programming language.<\/p>\n<p>So a closure is a function that uses a variable declare outside the function. Let&#8217;s take an example of the function below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> makeUp<span style=\"color: #333333;\">(<\/span>score<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Double<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> score <span style=\"color: #333333;\">+<\/span> bonus\r\n<\/pre>\n<p>In the example, we see a function called makeUp that takes a parameter, score. However, you also see that this function uses another variable called bonus. So where is the bonus coming from? It is a free variable. What??<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Free Variable<\/strong><\/p>\n<p>So what is heck is a <strong><em>&#8220;free variable&#8221;<\/em><\/strong>?\u00a0 It is not a parameter to the makeUp() function. Besides, it&#8217;s not also a local variable to the function. It is defined elsewhere outside the function such that the makeUp() function is free to use it. So we say the variable bonus is free variable to the makeUp() function.<\/p>\n<p>Of course, if you try to compile the code above, it throws an error.\u00a0 So you must have the variable defined as shown below for the code to compile.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demostrate Closure<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">HigherOrderFunctions<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> bonus<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>\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> makeUp<span style=\"color: #333333;\">(<\/span>score<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Double<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> score <span style=\"color: #333333;\">+<\/span> bonus\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: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">={<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">val<\/span> finalScore<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Double<\/span> <span style=\"color: #333333;\">=<\/span> makeUp<span style=\"color: #333333;\">(<\/span><span style=\"color: #6600ee; font-weight: bold;\">67.5<\/span><span style=\"color: #333333;\">);<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Final Score: \"<\/span> <span style=\"color: #333333;\">+<\/span> finalScore<span style=\"color: #333333;\">)<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>This works as expected and produces the following output<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Final Score: 87.5\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong>Topics on Functions in Scala<\/strong><\/h4>\n<ol>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-functions\/#t1\">Function Declaration and Definition<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-functions\/#t2\">Calling a Function<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-functions\/#t3\">Nested Functions<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-more-on-functions\/#t1\">Function Call by Name<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-more-on-functions\/#t2\">Functions With Variable Parameters<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-more-on-functions\/#t3\">Functions With Default Arguments<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-more-on-functions\/#t4\">Functions With Named Arguments<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-more-on-functions\/#t5\">Recursive Functions<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-yet-more-functions\/#t1\">Anonymous Functions<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-yet-more-functions\/#t2\">Partially Applied Functions<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-yet-more-functions\/#t3\">Higher-Order Functions (HOD)<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/scala\/scala-yet-more-functions\/#t4\">Currying Functions<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>A closure in Scala is a function and the concept of closure is quite clear. However I have decided to discuss closure under its own &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala - Closure - Scala Programming<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala - Closure - Scala Programming\" \/>\n<meta property=\"og:description\" content=\"A closure in Scala is a function and the concept of closure is quite clear. However I have decided to discuss closure under its own &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/\" \/>\n<meta property=\"og:site_name\" content=\"Scala Programming\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-21T10:07:06+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#\\\/schema\\\/person\\\/d69f73eca381222e2124581298dff5b4\"},\"headline\":\"Scala &#8211; Closure\",\"datePublished\":\"2022-01-21T10:07:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/\"},\"wordCount\":243,\"commentCount\":0,\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/\",\"name\":\"Scala - Closure - Scala Programming\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#website\"},\"datePublished\":\"2022-01-21T10:07:06+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#\\\/schema\\\/person\\\/d69f73eca381222e2124581298dff5b4\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-closure\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Closure\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/\",\"name\":\"Scala Programming\",\"description\":\"Scala Programing Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#\\\/schema\\\/person\\\/d69f73eca381222e2124581298dff5b4\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala - Closure - Scala Programming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/","og_locale":"en_US","og_type":"article","og_title":"Scala - Closure - Scala Programming","og_description":"A closure in Scala is a function and the concept of closure is quite clear. However I have decided to discuss closure under its own &hellip;","og_url":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/","og_site_name":"Scala Programming","article_published_time":"2022-01-21T10:07:06+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/scala\/#\/schema\/person\/d69f73eca381222e2124581298dff5b4"},"headline":"Scala &#8211; Closure","datePublished":"2022-01-21T10:07:06+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/"},"wordCount":243,"commentCount":0,"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/scala\/scala-closure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/","url":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/","name":"Scala - Closure - Scala Programming","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/scala\/#website"},"datePublished":"2022-01-21T10:07:06+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/scala\/#\/schema\/person\/d69f73eca381222e2124581298dff5b4"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/scala\/scala-closure\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/scala\/scala-closure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/scala\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Closure"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/scala\/#website","url":"https:\/\/kindsonthegenius.com\/scala\/","name":"Scala Programming","description":"Scala Programing Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/scala\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/scala\/#\/schema\/person\/d69f73eca381222e2124581298dff5b4","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/scala\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/215\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}