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 name : type . 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.
open class A { open fun foo(i: Int = 10) { …… } } class B : A() { override fun foo(i: Int) { ... } // cannot have default value }
fun foo(bar: Int = 0, baz: Int) { /* …… */ } foo(baz = 1) // use default value bar = 0
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 * 2Explicitly 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(){ }
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:: 10member function
class Sample() { fun foo() { print("Foo") } }Member functions are called in dot notation:
Sample().foo()