Scala method and function
Scala has methods and functions, and there is little semantic difference between the two. The Scala method is part of a class, and a function is an object that can be assigned to a variable. In other words, a function defined in a class is a method.
The methods in Scala are similar to those in Java, and they are part of the constituent class.
The function in Scala is a complete object, and the function in Scala is actually the object that inherits the class of Trait.
Used in Scala val
Statement can define a function def
statement defines the method.
class Test{
def m(x: Int) = x + 3
val f = (x: Int) => x + 3
}
Note: there is no difference between function and method in some translations.
Method declaration
The format of the Scala method declaration 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 that contains it is also an abstract type.
Method definition
The method definition is defined by a def
keyword, followed by an optional list of arguments, a colon: and the return type of the method, an equal sign =, and finally the body of the method.
The definition format of the Scala method is as follows:
def functionName ([parameter list]) : [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 method is to add and sum the two incoming parameters:
Example
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 be returned as Unit
, this is similar to that of Java void
the example is as follows:
Example
object Hello{
def printMe( ) : Unit = {
println("Hello, Scala!")
}
}
Method call
Scala provides several different method invocation methods:
The following is the standard format for calling methods:
functionName( parameter list )
If the method is called using the object of the instance, we can use a format similar to java (using the .
Number):
[instance.]functionName( parameter list )
The above example shows an example of defining and invoking methods:
Example
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 is as follows:
$ 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:
A case study of function concept Analysis |
|
---|---|
Function Call-by-Name |
Specify the function parameter name |
Function-variable argument |
Recursive function |
Default parameter value |
Higher order function |
Embedded function |
Anonymous function |
Partial application function |
Function Currying |