Functions and methods in Scala

A function is a group of statements that perform a task together. You can divide the code into different functions. It is up to you to decide how to divide the code into different functions, 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. Scala methods are part of a class, and functions are objects that can be assigned to a variable. In other words, the function defined in the class is the method.

We can define functions anywhere, even within functions (inline functions). More importantly, the Scala function name can have the following special characters: +, ++, ~, &,-, –,, /,: etc.

Function declaration format:
def functionName ([parameter list]): [return type]
Case:

object Function { 
 
  def main (args: Array [String]): Unit = { 
    println (fun01 ( 10 )) // function call 
    println (fun02 ( 10 , 20 )) // function call 
 
    // function passed as a parameter to Method: 
    val res = method02 ( 10 , fun02) 
    println (res) 
 
  } 
 
  // Function definition: val / var function name = (parameter list of function) => function body 
  val fun01 = (a: Int) => a + 10 
 
  // Define the function, with two parameters of type Int 
  val fun02 = (a: Int, b: Int) => {
     if (a> 10 || b> 10 ) a + b elseA - B 
  } 
 
 
  //   difference methods and functions:
   //   In functional programming languages, a function is a "first class citizen", it can be like any other data type
   // the same operations and is transferred 
 
 
  // function definition a sum 
  val fun_sum = (x: Int, y: Int) => x + y 
 
 
  // The number of parameters of function f is 1, the data type of function f is Int, and the return value of function f is Int 
  def method00 (a: Int, f: Int => Int) = { 
    a + f ( 10 ) 
  } 
 
 
  def method00 (a: Int, b: Int, c: Int) = {
     // After calling function f to sum 10 and 5, then sum Sum of parameter a 
    + b + c 
  } 
 
  // The number of parameters of function f is 2, the data type of the parameters of function f is Int, the return value of function f is Int 
  def method02 (a: Int, f: (Int , Int) => Int) = {
    // After calling the function f to sum 10 and 5, then sum the parameter a 
    var x = 10 
    var y = 5 
    val fvalues = f (x, y) // Call the function to achieve the sum of 10 and 5 
    a + fvalues 
  } 
 
 
}
 /**

  *  方法的声明和使用

  */

  

    
object Method {
 
  def main(args: Array[String]) {
    //调用:method01()或者method01
    //    method01
    //    method03
    println(method05("method05", 20))
  }
 
 
  /**
    * 使用def定义一个方法
    * 方法的定义:def 方法名(参数列表):返回值={方法体}
    */
  //def method07= 10
 
  def method01(): Unit = println("this is my first method")
 
 
  /**
    * 如果没有参数、可以直接不用写()
    */
  def method02 = println("this is my first method")
 
 
  /**
    * 如果没有参数、不确定返回值数据类型时
    */
  def method03 = {
    println("this is my first method")
  }
 
  /**
    * 指定方法的返回数据类型,方法的最后一个表达式作为方法的返回值
    */
  def method04: String = {
    "this is my first method"
  }
 
  /**
    * 当方法需要传入参数是时:
    *
    * @return
    */
  def method05(str: String, age: Int): String = {
    //把参数传入字符串里
    s"$str method,and age=$age"
  }
 
}
// 方法的参数列表  常用的参数有默认参数,带名参数和可变参数

    
object MethodArgment {
 
  def main(args: Array[String]) {
    //调用默认参数:
    method01("xingyue", 20, "nan")
    //  或者
    method01("qingyuan",22 )
 
    println("-----------------------------")
 
    //正常传递参数,根据参数列表顺序传递
    method02("laozhang", 40)
    //scala的带名参数:
    method02(age = 40, name = "laozhang")
 
    println("----------可变参数-------------------")
    method03(10, 20, 30, 40, 100, 1000)
 
  }
 
  /**
    * 默认参数
    * //我们在调用方法的时候,在某些情况下,没给参数,这个时候,
    * 方法会使用默认的一个参数,但是这个参数从哪里来?? 我在定义方法的时候就给了这个默认参数
    * //当我们给的参数不够的时候,会自动的依次补齐默认参数
    *
    * @param name
    * @param age
    * @param sex
    */
  def method01(name: String, age: Int, sex: String = "nan"): Unit = {
    val info = s"your name is $name,and age is $age,and sex is $sex"
    println(info)
 
  }
 
  /**
    * 带名参数:在调用函数的时候,我们不按照函数的定义参数列表来传递参数,
    * 使用的是带名参数的方式来传递
    *
    * @param name
    * @param age
    */
  def method02(name: String, age: Int): Unit = {
    val info = s"your name is $name,and age is $age"
    println(info)
 
  }
 
 
  /**
    * 可变参数:在定义函数时,可能无法确认出到底有多少个参数,可以用变长参数的形式来定义函数
    *
    * @param argInt
    */
  def method03(argInt: Int*): Unit = {
 
    //变量可变参数遍历方式1
    for (arg <- argInt.iterator)
      println(arg)
 
    //变量可变参数遍历方式2
    for (arg <- argInt)
      println(arg)
 
  }
 
  def method04(age: Int, str: String*): Unit = {
 
    //变量可变参数遍历方式1
    for (arg <- str.iterator)
      println(age + ":" + arg)
 
    //变量可变参数遍历方式2
    for (st <- str)
      println(age + ":" + st)
 
  }
 
}

 

Guess you like

Origin www.cnblogs.com/itboys/p/12750989.html