Kotlin | Default and Named argument

In most of the programming languages, we need to specify all the arguments that a function accepts while calling that function but in Kotlin, we need not specify all the arguments that a function accepts while calling that function so it is one of the most important features. We can get rid of this constraint and make parameter optional i.e pass an argument or not while calling a function.

In Kotlin, function parameters are separated using comma and defined using the Pascal notation i.e name:data_type.

fun fun_name(name1: data_type, name2: data_type )

There are two types of arguments in Kotlin –

  • Default arguments
  • Named arguments

Koltin Default arguments –

The arguments which need not specify explicitly while calling a function are called default arguments.
If the function is called without passing arguments then the default arguments are used as function parameters. In other cases, if arguments are passed during a function call then passed arguments are used as function parameters.



There are three cases for default arguments-

  1. No arguments are passed while calling a function
  2. Partial arguments are passed while calling a function
  3. All arguments are passed while calling a function

No arguments are passed while calling a function –

When no argument is passed while calling a function then the default arguments are used as function parameters. We need to initialize the variables while defining a function.

Kotlin program of calling student() function without passing an arguments –

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String>) {
    val name_of_student = "Gaurav"
    val standard_of_student = "VIII"
    val roll_no_of_student = 25
    student()         // passing no arguments while calling student
}

chevron_right


Output:

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

Explanation:
In the above program, we have used student function that accepts three arguments name, standard and roll_no. Note that we have initialized all the student arguments with some value. It is used to ensure that if nothing is passed in the student() while calling the function then these are the default values. Hence, in the above program, no arguments are passed so it uses the default arguments as function parameters and prints the default values to the standard output.

Partial arguments are passed while calling a function –

Here some of the arguments are passed while calling a function and these are used as function parameters. If any formal parameter does not get value from the function call then the default value will be used for that parameter.

Kotlin program of calling student() function with passing some arguments –

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String>) {
    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) 
}

chevron_right


Output:



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

Explanation:
In the above program, we have used student function that accepts three arguments name, standard and roll_no. Note that we have initialized all the student arguments with some value. Here, we have passed values only for name and standard of the student. So, for roll_no it will use the default value (11) and print all the values to the standard output as shown above.

All arguments are passed while calling a function –

Here, we have to pass all the arguments as defined in the function definition but data type of actual arguments must match with data type of formal arguments in same order.

Kotlin program of calling student() function with passing all the arguments –

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String>) {
    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)
}

chevron_right


Output:

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

Explanation:
In above program, we have passed all the arguments while calling the student() and it overwrites the default values for the function parameters. Hence, it prints only the values passes to formal arguments during the function call.

Kotlin Named arguments –

While working with the default arguments we face a problem. If we jumble the arguments then it will give compilation error so we have to pass the actual arguments to formal arguments in the same order as defined during function declaration.

Kotlin program for calling student() by passing arguments in random order-

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String>) {
    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)
}

chevron_right


Output:

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

In the above program, we have not passed the arguments in the order as these were defined in the function. So, it gives compilation error.

The arguments that are passed using name while calling a function are called named arguments. While calling the function we must use the name of the formal argument to which we are passing the actual argument value.

Kotlin program for calling student() with name of arguments –

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String>) {
    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)
}

chevron_right


Output:

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

Explanation:
Here, we passed the actual arguments using the name to the formal arguments. For only name and roll_no we have passed the values so it prints the default value of “standard of the student”.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.