Open In App

Kotlin | Default and Named argument

Improve
Improve
Like Article
Like
Save
Share
Report

In most 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 commas and defined using the Pascal notation i.e name:data_type. 

In Kotlin, default arguments allow you to specify a default value for a function parameter. This means that if the parameter is not explicitly passed in when the function is called, it will use the default value instead.

Example:

Kotlin




fun greet(name: String = "World") {
    println("Hello, $name!")
}
 
// Call with argument
greet("John") // Output: Hello, John!
 
// Call without argument
greet() // Output: Hello, World!


In this example, the greet function takes a single parameter name, which has a default value of “World”. When the function is called without an argument, it will use the default value.

Named arguments in Kotlin to allow you to pass arguments to a function by name, rather than by position. This can be useful when calling functions that have many parameters, or when you want to make the code more readable. 

Here’s an example:

Kotlin




fun printName(firstName: String, lastName: String) {
    println("First name: $firstName, Last name: $lastName")
}
 
// Call with named arguments
printName(lastName = "Doe", firstName = "John") // Output: First name: John, Last name: Doe


In this example, we’re calling the printName function with two named arguments: firstName and lastName. By using named arguments, we can pass the arguments in any order we want, which can make the code more readable.

fun fun_name(name1: data_type, name2: data_type )

There are two types of arguments in Kotlin –  

  1. Default arguments
  2. Named arguments

Kotlin 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 – 

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


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 –  

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<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)
}


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 the 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 the same order. 

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

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<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)
}


Output: 

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

Explanation: 

In the 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-  

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<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)
}


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 –  

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<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)
}


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”. 



Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads