📜  科特林 |默认和命名参数

📅  最后修改于: 2022-05-13 01:55:13.460000             🧑  作者: Mango

科特林 |默认和命名参数

在大多数编程语言中,我们需要指定函数在调用该函数时接受的所有参数,但在 Kotlin 中,我们不需要指定函数在调用该函数时接受的所有函数,因此这是最重要的特性之一.我们可以摆脱这个约束并使参数成为可选参数,即在调用函数时是否传递参数。
在 Kotlin 中,函数参数使用逗号分隔并使用 Pascal 表示法定义,即 name:data_type。

fun fun_name(name1: data_type, name2: data_type )

Kotlin 中有两种类型的参数——

  • 默认参数
  • 命名参数

Kotlin 默认参数 –

调用函数时不需要显式指定的参数称为默认参数。
如果在没有传递参数的情况下调用函数,则默认参数将用作函数参数。在其他情况下,如果在函数调用期间传递参数,则传递的参数将用作函数参数。
默认参数有三种情况-

  1. 调用函数时不传递任何参数
  2. 调用函数时传递部分参数
  3. 调用函数时传递所有参数

调用函数时不传递任何参数——

如果在调用函数时没有传递参数,则默认参数将用作函数参数。我们需要在定义函数时初始化变量。
Kotlin 程序在不传递参数的情况下调用 student()函数–

Kotlin
// default arguments in function definition name, standard and roll_no
fun student(name: String="Praveen", standard: String="IX" , roll_no: Int=11) {       
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
    student()         // passing no arguments while calling student
}


Kotlin
// default arguments in function definition name,standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
    // passing only two arguments name and standard of student
    student(name_of_student,standard_of_student)
}


Kotlin
// default arguments in function definition name, standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
 
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
 
    //passing all the arguments of student name,
    //standard and roll_no in same order as defined in function
    student(name_of_student,standard_of_student,roll_no_of_student)
}


Kotlin
// default arguments in function definition name,standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
 
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
    // passing the argument name_of_student to name
    // and roll_no_of_student to standard
    student(name_of_student,roll_no_of_student)
}


Kotlin
// default arguments in function definition
// name,standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
 
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
 
    // passing the arguments with name as defined in function
    student(name=name_of_student,roll_no=roll_no_of_student)
}


输出:

Name of the student is: Praveen
Standard of the student is: IX
Roll no of the student is: 11

解释:
在上面的程序中,我们使用了 student函数,它接受三个参数 name、standard 和 roll_no。请注意,我们已经用一些值初始化了所有学生参数。它用于确保如果在调用函数时没有在 student() 中传递任何内容,那么这些是默认值。因此,在上面的程序中,没有传递任何参数,因此它使用默认参数作为函数参数并将默认值打印到标准输出。

调用函数时传递部分参数 -

这里的一些参数是在调用函数时传递的,这些参数用作函数参数。如果任何形式参数没有从函数调用中获取值,则默认值将用于该参数。
Kotlin 程序调用 student()函数并传递一些参数 -

科特林

// default arguments in function definition name,standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
    // passing only two arguments name and standard of student
    student(name_of_student,standard_of_student)
}

输出:

Name of the student is: Gaurav
Standard of the student is: VIII
Roll no of the student is: 11

解释:
在上面的程序中,我们使用了 student函数,它接受三个参数 name、standard 和 roll_no。请注意,我们已经用一些值初始化了所有学生参数。在这里,我们只传递了学生的姓名和标准的值。因此,对于 roll_no,它将使用默认值 (11) 并将所有值打印到标准输出,如上所示。

所有参数都在调用函数时传递——

在这里,我们必须传递函数定义中定义的所有参数,但实际参数的数据类型必须与形式参数的数据类型以相同的顺序匹配。
Kotlin 程序调用 student()函数并传递所有参数 -

科特林

// default arguments in function definition name, standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
 
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
 
    //passing all the arguments of student name,
    //standard and roll_no in same order as defined in function
    student(name_of_student,standard_of_student,roll_no_of_student)
}

输出:

Name of the student is: Gaurav
Standard of the student is: VIII
Roll no of the student is: 25

解释:
在上面的程序中,我们在调用 student() 时传递了所有参数,它会覆盖函数参数的默认值。因此,它只打印函数调用期间传递给形式参数的值。

Kotlin 命名参数 –

在使用默认参数时,我们面临一个问题。如果我们混淆参数,那么它会产生编译错误,因此我们必须按照函数声明期间定义的顺序将实际参数传递给形式参数。
Kotlin 程序通过以随机顺序传递参数来调用 student() -

科特林

// default arguments in function definition name,standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
 
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
    // passing the argument name_of_student to name
    // and roll_no_of_student to standard
    student(name_of_student,roll_no_of_student)
}

输出:

Error:(12, 29) Kotlin: Type mismatch: inferred type is Int but String was expected

在上面的程序中,我们没有按照函数中定义的顺序传递参数。所以,它给出了编译错误。
在调用函数时使用名称传递的参数称为命名参数。在调用函数时,我们必须使用我们传递实际参数值的形式参数的名称。
使用参数名称调用 student() 的 Kotlin 程序 –

科特林

// default arguments in function definition
// name,standard and roll_no
fun student( name: String="Praveen", standard: String="IX" , roll_no: Int=11 ) {
    println("Name of the student is: $name")
    println("Standard of the student is: $standard")
    println("Roll no of the student is: $roll_no")
}
 
fun main(args: Array) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
 
    // passing the arguments with name as defined in function
    student(name=name_of_student,roll_no=roll_no_of_student)
}

输出:

Name of the student is: Gaurav
Standard of the student is: IX
Roll no of the student is: 25

解释:
在这里,我们使用名称将实际参数传递给形式参数。只有nameroll_no我们已经传递了值,所以它打印了“学生标准”的默认值。