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. The format of the Scala method declaration is as follows: 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. The method definition is defined by a The definition format of the Scala method is as follows: In the above code The function of the following method is to add and sum the two incoming parameters: If the method does not return a value, it can be returned as Scala provides several different method invocation methods: The following is the standard format for calling methods: If the method is called using the object of the instance, we can use a format similar to java (using the The above example shows an example of defining and invoking methods: Execute the above code, and the output is as follows: 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 8.15.1. Method declaration #
def functionName ([parameter list]) : [return type]
8.15.2. Method definition #
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.def functionName ([parameter list]) : [return type] = {
function body
return [expr]
}
return
type
can be any legal Scala data type. Parameters in the parameter list can be separated by commas.Example #
object add{
def addInt( a:Int, b:Int ) : Int = {
var sum:Int = 0
sum = a + b
return sum
}
}
Unit
, this is similar to that of Java
void
the example is as follows:Example #
object Hello{
def printMe( ) : Unit = {
println("Hello, Scala!")
}
}
8.15.3. Method call #
functionName( parameter list )
.
Number):[instance.]functionName( parameter list )
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
}
}
$ scalac Test.scala
$ scala Test
Returned Value : 12