Method 1.

1.1 Definition Method

grammar

[TIP]
1And parameter types cannot be omitted2, the return value type can be omitted and Scala will infer automatically3, the return value may not be writtenreturnThe default is the value of the {} block expressiondef methodName Parameter name: parameter type, parameter name: parameter type) : [return type] = {
    // Method body: a series of code
}
Copy the code

The sample

  1. Implement an addition operation

Reference code

// If no parameter type is written, an error will be reported
scala> def add(x,y) = {x + y}
<console>:1: error: ':' expected but ',' found.
def add(x,y) = {x + y}
         ^

scala> def add(x:Int,y:Int) = {x + y}
add: (x: Int, y: Int)Int

scala> add(3.4)
res1: Int = 7
Copy the code

1.2 Return value type inference

When defining a method, you can generally omit the return value type, but recursion does not

Reference code

scala> def m2(x:Int) = {|if(x<=1) 1
     | else m2(x- 1) * x
     | }
<console>:13: error: recursive method m2 needs result type
       else m2(x- 1) * x

// Add the return value type
scala> def m2(x:Int) :Int= {if(x<=1) 1 else x * m2(x- 1)}
m2: (x: Int)Int
Copy the code

1.3 Method Parameters

Method parameters in Scala are flexible to use. It supports the following types of parameters:

  • The default parameters
  • With name parameter
  • Variable-length argument

1.3.1 Default Parameters

You can define a default value for a parameter when you define a method.

// x, y with default values
scala> def add(x:Int=1,y:Int=3)= x+y
add: (x: Int, y: Int)Int

scala> add()
res4: Int = 4
Copy the code

1.3.2 Named Parameters

When calling a method, you can specify the name of the argument to make the call.

Reference code

scala> def add(x:Int=1,y:Int=3)= x+y
add: (x: Int, y: Int)Int
// specify x only. The default value of y is used
scala> add(x=2)
res5: Int = 5
Copy the code

1.3.3 Variable length Parameters

If the method’s arguments are not fixed, you can define a method whose arguments are variable-length arguments.

Syntax format:

// Add an '*' sign after the parameter type to indicate that the parameter can be zero or more
def The method name(Parameter name: Parameter type *): Return value type = {method body}Copy the code

Reference code

scala> def add(num:Int*) = num.sum
add: (num: Int*)Int

scala> add(1.2.3.4.5)
res1: Int = 15
Copy the code

1.4 Method invocation method

1.4.1 Suffix call method

grammar

Object name. Method name (parameter)Copy the code

Reference code

// Use the suffix 'math.abs' to find the absolute value
scala> Math.abs(- 1)
res3: Int = 1
Copy the code

1.4.2 Infix invocation method

grammar

Object name method name parameterCopy the code
scala> Math abs - 1
res4: Int = 1
Copy the code

Operators are methods

Let’s look at an expression

1 + 1
Copy the code

Does the above expression look like a method call to you?

In Scala, operators such as + – * / % are the same as in Java, but in Scala,

  • All operators are methods
  • An operator is a method whose name is a symbol

1.4.3 Curly bracket Call method

grammar

// the method takes only one argument to use curly braces
Math.abs{ 
    // Expression 1
    // Expression 2
}
eg:
scala> Math.abs{- 10}
res13: Int = 10
Copy the code

1.4.4 Call method without parentheses

If a method has no arguments, you can omit the parentheses following the method name

The sample

  • Define a method with no arguments, print “hello”
  • The method is called using the parenthesless invocation method

Reference code

scala> def noReturn() = println("I have no parameters.")
noReturn: ()UnitScala > noReturn I have no argumentsCopy the code

2. The function

2.1 Defining functions

grammar

1A function is an object (variable)2Like methods, functions have input parameters and return values3, function definitions do not need to use 'def`define
4, you do not need to specify the return value typevalFunction variable name = (Parameter name: parameter type, parameter name: parameter type....) = > the function bodyCopy the code

Reference code

scala> val add = (x:Int, y:Int) => x + y
add: (Int.Int) = >Int = <function2>

scala> add(1.2)
res3: Int = 3
Copy the code

2.2 Differences between methods and functions

  • Methods belong to a class or object and are loaded into the JVM’s method section at run time
  • You can assign a function object to a variable that is loaded into the JVM’s heap memory at run time
  • A function is an object that inherits from FunctionN, which has the apply, Curried, toString, tupled methods. Methods don’t

2.3 Methods into functions

  • Sometimes you need to convert a method to a function, and when you pass it as a variable, you need to convert a method to a function

  • Use _ to convert a method to a function

Reference code

scala> def add(x:Int,y:Int)=x+y
add: (x: Int, y: Int)Int

// Add space +_ after the method name
scala> val a = add _
a: (Int.Int) = >Int = <function2>
Copy the code