Kotlin Default and Named argument (original) (raw)

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

**Example:

Kotlin `

fun printName(firstName: String, lastName: String) {

println("First name: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>r</mi><mi>s</mi><mi>t</mi><mi>N</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo separator="true">,</mo><mi>L</mi><mi>a</mi><mi>s</mi><mi>t</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">firstName, Last name: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rs</span><span class="mord mathnormal" style="margin-right:0.10903em;">tN</span><span class="mord mathnormal">am</span><span class="mord mathnormal">e</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">L</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>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 the student() function without passing any arguments

Kotlin `

// default arguments in function definition name, standard and roll_no fun student(name: String="Ram", 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 = "Raj" val standard_of_student = "VIII" val roll_no_of_student = 25 student() // passing no arguments while calling student }

`

**Output:

Name of the student is: Ram
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 a value from the function call, then the default value will be used for that parameter.

**Kotlin program of calling the student() function with some arguments

Kotlin `

// default arguments in function definition name,standard and roll_no fun student( name: String="Ram", 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 = "Raj" 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: Raj
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 the data type of the actual arguments must match with data type of the formal arguments in the same order.

**Kotlin program of calling the student() function by passing all the arguments

Kotlin `

// default arguments in function definition name, standard and roll_no fun student( name: String="Ram", 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 = "Raj" 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: Raj
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 a 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) { 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:

Argument type mismatch: actual type is 'Int', but 'String' was expected.

In the above program, we have not passed the arguments in the order in which they were defined in the function. So, it gives a 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 a **student() with the name of the **arguments

Kotlin `

// default arguments in function definition // name,standard and roll_no fun student( name: String="Ram", 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 = "Raj" 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: Raj
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".