Named parameters
A named argument is a function called with a parameter attached to its argument:
fun sum(arg1: Int, arg2: Int) = arg1 + arg2
fun main(vararg args: String) {
sum(arg1 = 2, arg2 = 3)
sum(arg2 = 3, arg1 = 2)}Copy the code
Using named parameters can pass arguments to specified parameters precisely, so named parameters can be passed in no order.
Variable-length argument
Vararg allows a parameter to accept more than one value. Parameters modified by vararg are called variable length parameters:
fun hello(vararg ints: Int, string: String) {
ints.forEach(::println)
println(string)
}
fun main(vararg args: String) {
// the IDE reported an error: Type mismatch. Required:Int Found:String
hello(1.2.3.4.5, string = "hello")}Copy the code
- Variable length arguments may not be the last argument
- If there is any ambiguity, use a named parameter
Spread Operator
When variable length arguments need to be passed in an array, we can use the expansion operator *, which will pass the array one by one:
val array = intArrayOf(1.2.3.4.5)
hello(*array, string = "Hello") // In the vararg variable length scenario, asterisks can expand arrays, but lists are not supported
Copy the code
The expansion operator * has the following characteristics:
- Only Array is supported
- Arguments to variable length argument lists only
- This operator is not normal and cannot be overloaded
The default parameters
Default arguments, which can be used to specify default values for arguments anywhere in the function:
fun say(age: Int = 20, name: String, height: Float){... }fun main(vararg args: String) {
say(18."lqr".2.0 f)
say(name = "lqr", height = 2.0 f)}Copy the code
If the default parameter causes ambiguity in parameter transmission, use a named parameter. In other words, the parameter after the default parameter needs to be passed as a named parameter.
The return value more
With Pair and Triple you can implement functions that return multiple values because Pair and Triple support destruct syntax:
fun main(vararg args: String) {
val (myName, myAge) = multiReturnValues()
println("myName is $myName, myAge is $myAge")
val (name, age, height) = multiReturnValues(180.0)
println("name is $name, age is $age, height is $height")}fun multiReturnValues(a): Pair<String, Int> {
// return Pair("lqr", 18)
return "lqr" to 18
}
fun multiReturnValues(height: Double): Triple<String, Int.Double> {
return Triple("lqr".18, height)
}
Copy the code
To is an infix expression that returns a Pair of keys and values in the Map.
Catch exceptions
Kotlin support try… Catch or try… catch… Finally catches an exception, the catch branch matches the exception type, and finally executes whether or not the code throws an exception:
try {
val arg1 = args[0].toInt()
val arg2 = args[1].toInt()
println("$arg1 + $arg2 = ${sum(arg1, arg2)}")}catch (e: NumberFormatException) {
println("The input data is not a number.")}catch (e: ArrayIndexOutOfBoundsException) {
println("You need to enter two numbers.")}catch (e: Exception) {
println("Unknown exception occurs:${e.message}")}finally {
println("Thanks for using")}Copy the code
Like if and when, try… Catch can also be an expression that can be used to assign:
val result = try {
args[0].toInt() / args[1].toInt()
} catch (e: Exception) {
0
}
Copy the code