Tuesday 1 September 2020

Scala: Named Parameters

When calling methods, you can label the arguments with their parameter names like so.


For example, I defined a function fullName, that takes firstName, lastName of a person as arguments, and return full name.

 

def fullName(firstName: String, lastName: String) : String = {

     "(" + firstName + "," + lastName + ")"

}

 

You can call the function like below.

fullName("Ram", "Gurram")

fullName(lastName="Gurram", firstName="Ram")

 

Example

scala> def fullName(firstName: String, lastName: String) : String = {
     |   "(" + firstName + "," + lastName + ")"
     | }
def fullName(firstName: String, lastName: String): String

scala> 

scala> fullName("Ram", "Gurram")
val res79: String = (Ram,Gurram)

scala> fullName(lastName="Gurram", firstName="Ram")
val res80: String = (Ram,Gurram)




Previous                                                    Next                                                    Home

No comments:

Post a Comment