Scala functions


A function is a group of statements that perform a task together. You can divide your code into different functions. How you divide your code into different functions is up to you, but logically the division is usually based on each function performing a specific task.

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

We can define functions anywhere, even within functions (inline functions). The more important point is that Scala function names can contain the following special characters: +, ++, ~, &,-, -- , \, /, :, etc.


Function declaration

Scala function declaration format is as follows:

def functionName ([参数列表]) : [return type]

If you do not write the equal sign and method body, then the method will be implicitly declared as "abstract" (abstract)", the type containing it is also an abstract type.


Function definition

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

The Scala function definition format is as follows:

def functionName ([参数列表]) : [return type] = {
   function body
   return [expr]
}

In the above code, return type can be any legal Scala data type. Parameters in the parameter list can be separated by commas.

The function of the following function is to add the two incoming parameters and sum them up:

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

      return sum
   }
}

If the function does not return a value, it can be returned to Unit, which is similar For Java's void, the examples are as follows:

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

Function call

Scala provides a variety of different function calling methods:

The following It is the standard format for calling methods:

functionName( 参数列表 )

If the function uses an instance object to call, we can use a java-like format (using .):

[instance.]functionName( 参数列表 )

The above example demonstrates the definition and calling of functions:

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, and the output result is:

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

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

Function concept analysis and case study
Call-by-NameSpecify the function parameter name
Function- Variable parametersRecursive function
Default parameter valueHigher-order function
Inline functionAnonymous function
Partial application function Function Currying