0 votes
in Scala Constructs by
Explain how you will explain a function in Scala.

1 Answer

0 votes
by

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 

...