Latest web development tutorials

Scala function

Is a set of functions to perform a mission statement together. You can put the code into different functions. How to divide the code into separate functions is up to you to decide, but logically divided usually perform each function based on a specific task to carry out.

Scala has functions and methods, both on the small difference in semantics. Scala method is part of a class, and the function is an object that can be assigned to a variable. In other words, the function defined in a class that is a method.

We can define functions anywhere, even defined functions (inline function) within the function. The more important point is Scala function name may consist of the following special characters:+, +, -, &, -, -, \, /,:, etc.

Function declaration

Scala function declaration in the following format:

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

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

Function definition

The method is defined by a start def keyword, followed by an optional list of parameters, a colon: and return type of the method, a number equal to "=", and finally the body of the method "."

Scala function is defined in the following format:

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

The above codereturn type can be any valid data type Scala.Parameter list parameters can be separated by commas.

Features The following function is passed two parameters are summed and the summation:

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, you can return theUnit, this is similar to Java's void,examples are as follows:

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

Function call

Scala offers a variety of different ways to call the function:

The following is the standard format method call:

functionName( 参数列表 )

If the function is used to call the object instance, we can use a similar java format (using thenumber.):

[instance.]functionName( 参数列表 )

The above examples demonstrate the definition and calling the function instance:

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

Implementation of the above code, the output is:

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

Scala is a functional language, so the function is the core of the Scala language. Here are some function concept will help us better understand the Scala programming:

Analytic function concept connection Case
Function by name calling (Call-by-Name) Specifies the name of the function arguments
Function - variable parameter Recursive function
The default parameter values Higher-order functions
Nested functions Anonymous function
Partial function application Curried function (Function Currying)