{"id":8638,"date":"2021-05-25T14:24:01","date_gmt":"2021-05-25T14:24:01","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8638"},"modified":"2024-04-13T12:02:46","modified_gmt":"2024-04-13T12:02:46","slug":"scala-varargs","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/","title":{"rendered":"Scala &#8211; Var,Args"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Understanding the VarArgs in Scala&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Understanding the VarArgs in Scala<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Var-Args<\/strong> Functions(Methods) implies <a href=\"http:\/\/scala-lang.org\">taking<\/a> Variable Number of Arguments. As a Java Developer, We have effectively tasted use of Var-Args strategies in Java-based Applications.<\/p>\r\n\r\n\r\n\r\n<p>Like Java, <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\">Scala<\/a> likewise upholds &#8220;Variable Number Of Arguments&#8221; to Functions(Methods). Scala adheres to same guidelines like Java&#8217;s Var-Args Methods with some language structure changes.<\/p>\r\n<p>In Scala, VarArgs (variable-length arguments) provide a convenient way to define functions that accept a variable number of arguments of the same type. VarArgs allow functions to be with any number of arguments, including none, making them flexible and versatile.<\/p>\r\n<p>To declare a VarArgs parameter in Scala, use the syntax <code>args: Type*<\/code>, where <code>Type<\/code> is the type of the arguments accept. Within the function body, <code>args<\/code> behaves like a sequence (<code>Seq<\/code>) of elements of type <code>Type<\/code>, allowing you to iterate over the arguments using standard collection operations.<\/p>\r\n<p>When invoking a function with VarArgs, you can pass zero or more arguments of the specifiey type, which will be automatically wrapp into a sequence by the Scala compiler. For example, a function <code>sum(numbers: Int*)<\/code> can be with <code>sum(1, 2, 3)<\/code> to calculate the sum of the provid integers.<\/p>\r\n<p>Under the hood, VarArgs are implemented using arrays (<code>Array<\/code>) in Scala, providing efficient performance for functions with a varying number of arguments.Understanding the VarArgs in Scala<\/p>\r\n\r\n\r\n\r\n<h4><strong>use case 1:<\/strong><\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\/\/ Scala program of varargs\r\nobject prwatech {\r\n  \/\/ Driver code\r\n    def main(args: Array[String])\r\n    { \r\n              \r\n        \/\/ Calling the function \r\n        println(\"Sum is: \" + sum(2, 3, 100, 200, 30)); \r\n    } \r\n      \r\n      \r\n    \/\/ declaration and definition of function \r\n    def sum(a :Int, b :Int, args: Int *) : Int =\r\n    {\r\n        var result = a + b\r\n      \r\n        for(arg &lt;- args)\r\n        {\r\n            result += arg\r\n        }\r\n      \r\n        return result\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Sum is: 335<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\/\/ Scala program of varargs\r\nobject prwatech {\r\n \/\/ Driver code\r\n    def main(args: Array[String]) \r\n    {\r\n      \r\n        \/\/ calling of function     \r\n        printPrwa(\"Prwatech\", \"Data\", \"Science\")\r\n  \r\n    }\r\n  \r\n    \/\/ declaration and definition of function\r\n    def printPrwa(strings: String*) \r\n    {\r\n        strings.map(println)\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Prwatech\r\nData\r\nScience\r\n\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>object prwatech {\r\ndef main(args: Array[String]) {\r\n      printStrings(\"Hello\", \"Scala\", \"Python\");\r\n   }\r\n   \r\n   def printStrings( args:String* ) = {\r\n      var i : Int = 0;\r\n      \r\n      for( arg &lt;- args ){\r\n         println(\"Arg value[\" + i + \"] = \" + arg );\r\n         i = i + 1;\r\n      }\r\n   }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Arg value[0] = Hello\r\nArg value[1] = Scala\r\nArg value[2] = Python\r\n<\/code><\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>Understanding the VarArgs in Scala &nbsp; Var-Args Functions(Methods) implies taking Variable Number of Arguments. As a Java Developer, We have effectively tasted use of Var-Args strategies in Java-based Applications. Like Java, Scala likewise upholds &#8220;Variable Number Of Arguments&#8221; to Functions(Methods). Scala adheres to same guidelines like Java&#8217;s Var-Args Methods with some language structure changes. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1391,1389,1396,1397,1388,1392,1393,1394,1395,1390],"class_list":["post-8638","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-how-var-args-is-used-in-scala","tag-scala-var-args","tag-scala-var-args-example","tag-scala-var-args-implementattion","tag-var-args","tag-var-args-in-scala","tag-var-args-sample-program","tag-var-args-sample-use-cases","tag-var-args-uses-in-scala","tag-what-is-var-args-in-scala"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding the VarArgs in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Understanding VarArgs in Scala - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding the VarArgs in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Understanding VarArgs in Scala - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-25T14:24:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T12:02:46+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/\",\"name\":\"Understanding the VarArgs in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2021-05-25T14:24:01+00:00\",\"dateModified\":\"2024-04-13T12:02:46+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Understanding VarArgs in Scala - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Var,Args\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Understanding the VarArgs in Scala - Prwatech","description":"Master Understanding VarArgs in Scala - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Understanding the VarArgs in Scala - Prwatech","og_description":"Master Understanding VarArgs in Scala - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-25T14:24:01+00:00","article_modified_time":"2024-04-13T12:02:46+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/","name":"Understanding the VarArgs in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2021-05-25T14:24:01+00:00","dateModified":"2024-04-13T12:02:46+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Understanding VarArgs in Scala - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-varargs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Var,Args"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=8638"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8638\/revisions"}],"predecessor-version":[{"id":11461,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8638\/revisions\/11461"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}