

This article contains example of Scala function named arguments or we can say change sequence of argument while function calling. Scala support named arguments while java not. Let try to understood what is named arguments? Using name arguments we can change order of arguments while calling function. So not like java in scala is not compulsory to pass function argument as same as declaration of function.
Table of Contents
ScalaNamedArgumentDemo.scala
This example we have call same function with different-different ways:
object ScalaNamedArgumentDemo { def main(args: Array[String]): Unit = { printDetails("Zone", 27); // without named argument printDetails(name = "Zone", age = 27); // with named arguments printDetails(age = 27, name = "Zone") // with named arguments printDetails(name = "Zone", 27); // possible to skip name but at that skip arguments must be same sequence location at declare printDetails("Zone", age = 27); // possible to skip name but at that skip arguments must be same sequence location at declare // printDetails( age = 27,"Zone"); // not possible, } def printDetails(name: String, age: Int): Unit = { println("My Name is" + name + " and age is " + age) } }
Compile & Run & Output
Compile – scalac ScalaNamedArgumentDemo.scala
Run – scala ScalaNamedArgumentDemo
Output:
My Name is Zone and age is 27 My Name is Zone and age is 27 My Name is Zone and age is 27 My Name is Zone and age is 27 My Name is Zone and age is 27
References:
Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.