Kotlin series six - function

Overview: The use and definition of functions in kotlin is more flexible and diverse. In addition, it supports different degrees of expansion, which is convenient for developers to create their own methods and improve development efficiency. Here is a brief introduction to the use of different functions. This chapter is introduced together with extended attributes.

statement:

Functions in Kotlin are declared with the fun keyword:

fun double(x: Int): Int {
    return 2 * x
}
Function usage
Calling a function uses the traditional method:

val result = double(2)
Calling a member function uses dot notation:

Sample().foo() // Create instance of class Sample and call foo

parameter

Function parameters are defined using Pascal notation, ie  nametype . Parameters are separated by commas. Each parameter must have an explicit type:

fun powerOf(number: Int, exponent: Int) {
……
}

default parameters

Function parameters can have default values, which are used when the corresponding parameter is omitted. This reduces the number of overloads compared to other languages:

fun read(b: Array<Byte>, off: Int = 0, len: Int = b.size) {
……
}

The default value is defined by the  = following the type  and the value given.

Overriding methods always use the same default parameter values ​​as the base type method. When overriding a method with default parameter values, the default parameter values ​​must be omitted from the signature:
open class A {
open fun foo(i: Int = 10) { …… } }
class B : A() {
override fun foo(i: Int) { ... } // cannot have default value }
If a default parameter precedes a parameter without a default value, then the default value can only be used by calling the function with named parameters:
fun foo(bar: Int = 0, baz: Int) { /* …… */ }

foo(baz = 1) // use default value bar = 0
If the last lambda expression parameter is passed outside the parentheses to the function call, then default parameters are allowed to pass no value:
fun foo(bar: Int = 0, baz: Int = 1, qux: () -> Unit) { /* …… */ }
foo(1) { println("hello") } // use default value baz = 1 foo { println("hello") }
When a function call mixes positional and named parameters, all positional parameters are placed before the first named parameter. For example, a call to f(1, y = 2) is allowed but f(x = 1, 2) is not allowed, a function with a block body must always specify a return type explicitly.

variable parameter

foo( string = *arrayOf("1","2"))
//foo( "1","2")
fun foo(vararg string: String){

}

single expression function

When a function returns a single expression, you can omit the curly braces and specify the code body after the = sign:
fun double(x: Int): Int = x * 2
Explicitly declaring the return type is optional when the return type can be inferred by the compiler:
fun double(x: Int) = x * 2

generic function

Functions can have generic parameters, specified by using angle brackets before the function name, as described in Generics earlier:
fun <T> singletonList(item: T): List<T> {
    // ……
}

function scope

In Kotlin functions can be declared at the top level of the file, which means you don't need to create a class to hold a function like in some languages ​​like Java, C# or Scala. In addition to top-level functions, functions in Kotlin can also be declared in local scopes, as member functions and extension functions. We create methods in kotlin files separately without creating classes and packages.
fun action(){
 
}
local function
A double() method is created in the add() method, and the times of the external function are used in the double() method to calculate the magnification:
fun add( a: Int ,  b:Int) : Int{
        var  times = 2
        fun twice() : Int{
          val  c = a * times + b * times
            Log.e("result====",c.toString())
            return c
        }
        return  twice()
    }
transfer:
val result = add(2, 3)
Log.e("result:", result.toString())
Output result:
02-07 11:55:52.019 14759-14759/com.example.administrator.kotlinpractise E/result====: 10
02-07 11:55:52.019 14759-14759/com.example.administrator.kotlinpractise E/result:: 10
member function
Member functions are functions defined inside a class or object:
class Sample() {
    fun foo() { print("Foo") }
}
Member functions are called in dot notation:
Sample().foo()














Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325773273&siteId=291194637