1.

Explain how you will explain a function in Scala.

Answer»

Functions are first-class values in Scala and can be CREATED using the def keyword. When defining a function, the return type of the parameter must be SPECIFIED. A function's return type is optional. The default return type of a function is Unit if it is not specified. Function declarations in Scala have the following FORM: − 

 def function_name ([list of parameters]) : [return type] = { //Statement to be executed }

When you don't use the EQUALS sign and the method body, methods are implicitly declared abstract. 

Example: 

 object InterviewBit { def main(args: Array[String]) { println("Sum:" + addFunction(10,5)); } def addFunction(x:Int, y:Int) : Int = { var sum:Int = 0 sum = x + y return sum } }

Output: 

 Sum: 15


Discussion

No Comment Found