function
function Declaration
Functions in Kotlin Use the FUN keyword declaration
Fun double (x:int): Int {
}
function Usage
Calling a function using the traditional method
Val result = double (2)
Call member functions using dot notation
Sample (). Foo ()//Create class sample instance and call Foo
infix notation
Functions can also be called with infix notation when they are member functions or extension functions they have only one parameter they use the infix keyword to annotate
Define an extension to int
infix fun INT.SHL (x:int): int {
...
}
Call extension function with infix notation
1 SHL 2
//equal to this
1.SHL (2)
Parameters
function parameters are defined by Pascal notation, which is name:type. Parameters are separated by commas. Each parameter must have an explicit type.
Fun Powerof (Number:int, exponent:int) {
...
}
Default Parameters
A function parameter can have a default value, and the default value is used when the corresponding argument is omitted. This can reduce 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 = and the value given after the type.
The overriding method always uses the same default parameter value as the base type method. When overriding a method with a default parameter value, you must omit the default parameter value from the signature:
Open Class A {
open fun foo (i:int = ten) {...}
}
Class B:a () {
override fun foo (i:int) {...} Cannot have default value
}
named parameters
You can use named function arguments when calling a function. This is handy when a function has a large number of parameters or default parameters.
Given the following function
Fun reformat (str:string,
Normalizecase:boolean = True,
Uppercasefirstletter:boolean = True,
Dividebycamelhumps:boolean = False,
Wordseparator:char = ") {
...
}
We can use the default parameters to invoke it
Reformat (str)
However, when it is called with a non-default argument, the call looks like
Reformat (str, True, True, False, ' _ ')
Using named parameters we can make the code more readable
Reformat (str,
normalizecase = True,
Uppercasefirstletter = True,
Dividebycamelhumps = False,
Wordseparator = ' _ '
)
And if we don't need all the parameters
Reformat (str, wordseparator = ' _ ')
Note that you cannot use named parameter syntax when calling Java functions, because Java bytecode does not always retain the name of a function parameter. function to return Unit
If a function does not return any useful value, its return type is Unit. A Unit is a type that has only one value--unit. This value does not require an explicit return
Fun Printhello (name:string): Unit {
if (name! = null)
println ("Hello ${name}")
else
println ("Hi there !")
' Return Unit ' or ' return ' is optional
}
The Unit return type declaration is also optional. The above code is equivalent to
Fun Printhello (name:string?) {
...
}
single-expression functions
When a function returns a single expression, you can omit the curly braces and specify the code body after the = symbol
Fun double (x:int): Int = x * 2
When the return value type can be inferred by the compiler, it is optional to explicitly declare the return type
Fun double (X:int) = x * 2
an explicit return type
Functions that have block code bodies must always explicitly specify the return type, unless they are intended to return a Unit, in which case it is optional. Kotlin does not infer the return type of a function that has a block code body, because such a function may have complex control flows in the code body, and the return type is not obvious to the reader (and sometimes even to the compiler). variable number of parameters (Varargs)
The parameters of the function (usually the last one) can be marked with the vararg modifier:
Fun <T> aslist (vararg ts:t): list<t> {
val-result = Arraylist<t> () for
(T-TS)//TS is an Ar Ray
Result.add (t)
return result
}
Allows you to pass a variable number of arguments to a function:
Val list = Aslist (1, 2, 3)
Inside the function, the vararg parameter of type T is visible as a T array, that is, the TS variable in the above example has the type array <out t>.
Only one parameter can be labeled as vararg. If the vararg parameter is not the last parameter in the list, you can use the named argument syntax to pass the value of the parameter after it, or, if the argument has a function type, pass a lambda outside the parentheses.
When we call the vararg-function, we can pass the arguments one by one, such as Aslist (1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the stretch (spread) operator (preceded by * in the array):
Val A = arrayof (1, 2, 3)
val list = aslist ( -1, 0, *a, 4)