Scala - Functions

Scala has methods and functions, and the semantic differences between the two are small. A Scala method is part of a class, and a function is an object that can be assigned to a variable. In other words, the function defined in the class is the method.

The methods in Scala are similar to Java's, in that they form part of a class.

The function in Scala is a complete object, and the function in Scala is actually an object that inherits Trait's class.

The val statement in Scala defines the function, and the def statement defines the method.

class Test{
  def m(x: Int) = x + 3
  val f = (x: Int) => x + 3

Note: Some translations have no difference between function and method.

Method declaration

The Scala method declaration format is as follows:

def functionName ([parameter list]) : [return type]

If you don't write the equals sign and the method body, the method is implicitly declared as abstract, and the type containing it is also an abstract type.

method definition


method definition begins with a def keyword, followed by an optional parameter list, a colon : and the return type of the method, one The equal sign = , and finally the body of the method.

The Scala method definition format is as follows:

def functionName ([parameter List]) : [return type] = {
   function body
   return [expr]

The return type in the above code can be any legal Scala data type. The parameters in the parameter list can be separated by commas.

The function of the following method is to add and sum the two passed parameters:

object add{
   def addInt( a:Int, b:Int ) : Int = {
      var sum:Int = 0
      sum = a + b

      return sum

If the method doesn't return a value, you can return Unit, which is similar to Java's void. The example is as follows:

object Hello{
   def printMe( ) : Unit = {
      println("Hello, Scala!")

Method Call

Scala provides a number of different method invocations:

The following is the standard format for calling methods:

functionName( Parameter List )

If the method is called with an instance object, we can use a java-like format (using the . number):

[instance.]< /span>functionName( parameters List )

The above example demonstrates an example of defining and calling methods:

object Test {
   def main(args: Array[String]) {
        println( "Returned Value : " + addInt(5,7) );
   def addInt( a:Int, b:Int ) : Int = {
      var sum:Int = 0
      sum = a + b

      return sum

Execute the above code, the output is:

$ scalac Test.scala 
$ scala Test
Returned Value : 12

Scala is also a functional language, so functions are at the heart of the Scala language. The following functional concepts help us better understand Scala programming:

function concept analysis case
Call-by-Name Specify function parameter names
Function - Variable Arguments Recursive Functions
Default parameter values higher order function Functions
nested functions Anonymous Functions
Partial Application Functions Function Currying (Function Currying)