English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Methods and Functions in Scala

Scala has methods and functions, and there is a very small difference in semantics between them. Scala methods are part of a class, while functions are objects that can be assigned to a variable. In other words, functions defined in a class are methods.

Scala's methods are similar to Java's, as methods are part of a class.

In Scala, a function is a complete object, and the function in Scala is actually an object of a class that inherits the Trait.

Scala uses val statement can define a function,def statement defines a method.

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

Note:In some translations, function(function) and method(method) are not distinguished.

method declaration

The format of Scala method declaration is as follows:

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

If you do not write the equal sign and the method body, the method will be implicitly declared asabstract(abstract)which is also an abstract type, including its type.

method definition

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

The format of Scala method definition is as follows:

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

The following code includes return type It can be any valid Scala data type. The parameters in the parameter list can be separated by commas.

The following methods are used to add two input parameters and calculate the sum:

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

If the method does not return a value, it can return as UnitThis is similar to Java's voidThe following is an example:

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

Method Invocation

Scala provides various ways to invoke methods:

The following is the standard format for invoking methods:

functionName( parameters list )

If the method uses an example object to invoke, we can use a format similar to Java (using . number):

[instance.]functionName( parameters list )

The following example demonstrates the definition and invocation of 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
   }
}

After executing the above code, the output is:

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

Scala is also a functional programming language, so functions are at the core of the Scala language. Some concepts of functions can help us better understand Scala programming:

Understanding Function Concepts with Examples
Function Call by Name (Call-by-Name)Named Parameters for Functions
Functions - Variable ArgumentsRecursive Functions
Default Parameter ValuesHigher-Order Functions
Nested FunctionsAnonymous Functions
Partial Application of FunctionsFunction Currying (Function Currying)