Multiple Argument List : Example

A method will multiple argument list are also Variadic methods
scala> // #PS:37

scala> // When a function is defined with Multiple argument list(getMark()

scala> // in this case), 

scala> // (1) : last argument can be passed using curly bracket,

scala> // which gives a nice looking block syntax instead of using Round 

scala> // bracket

scala> //(2) : Implicit arguments can be passed for last argument list

scala> 

scala> // getMark() is defined to take 3 arguments

scala> // Arg 1 : String

scala> // Arg 2 : Int

scala> // Arg 3 : Function

scala> def getMark(subject: String, mark:Int)(printFn: (String, Int) => String) = {
     |   val finalMark = if (subject == "math") {
     |                     mark + 10
     |                   }else {
     |                     mark  
     |                   }
     |   printFn(subject, finalMark)                  
     | }
getMark: (subject: String, mark: Int)(printFn: (String, Int) => String)String

scala> 

scala> // Calling getMark() : Example 1 ( Last argument passed using curly bracket)

scala> getMark("math", 80){
     |                       (subject:String, mark:Int) => {
     |                         s"$subject mark is -> $mark"
     |                       }
     |                     }
res33: String = math mark is -> 90

scala> 

scala> // Calling getMark() : Example 2

scala> getMark("science", 70){ 
     |                         (subject:String, mark:Int) => {
     |                           if(mark <= 80) {
     |                             s"$subject mark is low -> $mark"  
     |                           }else {
     |                             s"$subject mark is good -> $mark"
     |                           }
     |                         }
     |                       }
res34: String = science mark is low -> 70

No comments:

Post a Comment