Scala – Function with named parameters
In Scala, we can write the function with default parameters as we have seen in the last post.Now, we will see how to pass parameters with some name. We can pass named parameters in any order and can also pass parameters without name. Let’s understand with an example.
object FuncWithDefaultParam { def main(args: Array[String]): Unit = { println("a=2,b=3 - Result: "+addition(a=2,b=3)) //Passed parameter names in same sequence /Result : 2 + 3 println("b=3,a=2 - Result: " + addition(b=3,a=2)) //Passed parameters names with changed sequence/Result : 2 + 3 println("2,3 - Result: " +addition(2,3)) //Passing only values /Result : 2 + 3 } def addition(a:Int=0,b:Int=0): Int ={ a + b } }
a=2,b=3 - Result: 5
b=3,a=2 - Result: 5
2,3 - Result: 5