In this article, you will learn about default and named arguments with the help of examples.
Kotlin Default Argument
In Kotlin, you can provide default values to parameters in the function definition and the parameter with a default value is called default argument.
There are three cases for default arguments:
Case I: All arguments passed
When all arguments are passed during a function call then passed arguments are used as function parameters.
The data type of actual arguments must match with the data type of formal arguments (arguments defined in the function definition) in same order.
Example:
//employee() definition //emp_id, emp_fname, emp_lname, emp_salary are default arguments fun employee(emp_id: Int = 1, emp_fname: String = "Arun", emp_lname: String = "Verma", emp_salary: Long = 45000) { println("Employee Name: $emp_fname $emp_lname") println("Employee Id: $emp_id") println("Employee Salary: $emp_salary") } fun main(args: Array<String>) { //passing all arguments while calling employee() employee(4, "Ramesh", "Singh", 50000) }
Output:
Employee Name: Ramesh Singh Employee Id: 4 Employee Salary: 50000
Example explained:
- employee() definition, accepts four default arguments ie. emp_id, emp_fname, emp_lname and emp_salary.
- During employee() call, if we pass all the arguments then these values will overwrite the default values for the function parameters.
- Hence, it prints only the values passed during the function call to the standard output.
Case II: Partial arguments are passed
When some of the arguments are passed while calling a function then these passed arguments 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.
Example:
//employee() definition //emp_id, emp_fname, emp_lname, emp_salary are default arguments fun employee(emp_fname: String = "Arun", emp_lname: String = "Verma", emp_id: Int = 1, emp_salary: Long = 45000) { println("Employee Name: $emp_fname $emp_lname") println("Employee Id: $emp_id") println("Employee Salary: $emp_salary") } fun main(args: Array<String>) { //passing three arguments emp_fname, emp_lname, emp_id //while calling employee() employee("Manisha", "Thakur", 2) }
Output:
Employee Name: Manisha Thakur Employee Id: 2 Employee Salary: 45000
Example explained:
- employee() definition, accepts four default arguments ie. emp_id, emp_fname, emp_lname and emp_salary.
- During employee() call, we have passed values only for emp_fname, emp_lname, emp_id and for emp_salary it will use the default value (45000).
- print all the values to the standard output.
Case III: No argument is passedÂ
When no argument is passed while calling a function then the default arguments are used as function parameters.
Example:
//employee() definition //emp_id, emp_fname, emp_lname, emp_salary are default arguments fun employee(emp_id: Int = 1, emp_fname: String = "Arun", emp_lname: String = "Verma", emp_salary: Long = 45000) { println("Employee Name: $emp_fname $emp_lname") println("Employee Id: $emp_id") println("Employee Salary: $emp_salary") } fun main(args: Array<String>) { //passing no arguments while calling employee() employee() }
Output:
Employee Name: Arun Verma Employee Id: 1 Employee Salary: 45000
Example explained:
- employee() definition, accepts four default arguments ie. emp_id, emp_fname, emp_lname and emp_salary.
- During employee() call, no arguments are passed so it uses the default arguments as function parameters.
- prints the default values to the standard output.
Kotlin Named Argument
In Kotlin, function parameters can be named when calling functions. The arguments that are passed using name while calling a function are called named arguments.
This is very convenient when a function has a high number of parameters or default ones.
Note: While calling function, we must use the name of the formal argument to which we are passing the actual argument value and using named arguments, now we can pass actual arguments in random order.
Example:
calculateAmount(20000,1000,500)
Suppose calculateAmount() definition exist somewhere else in your program. When a programmer reads the above line he may confused, which value represents what.
To avoid this confusion, function parameters can be named when calling functions.For example:
calculateAmount(balance=20000, withdraw_amount=1000, deposit_amount=500)
Another benefit of using Named Argument comes while working with the default arguments.
During function call, if we pass the arguments in random order then it will give compilation error so we have to pass the actual arguments in the same order as defined during function declaration.
Example:
fun employee(emp_fname: String = "Arun", emp_lname: String = "Verma", emp_id: Int, emp_salary: Long) { println("Employee Name: $emp_fname $emp_lname") println("Employee Id: $emp_id") println("Employee Salary: $emp_salary") } fun main(args: Array<String>) { //compilation error //passing values in random order employee("Asha", 5, "Verma", 23000)
In the above program, we have not passed the arguments in the order as these were defined in the function definition. So, it gives compilation error.
Note: If a default parameter precedes a parameter with no default value, the default value can only be used by calling the function with named arguments:
//employee() definition fun employee(emp_fname: String = "Arun", emp_lname: String = "Verma", emp_id: Int, emp_salary: Long) { println("Employee Name: $emp_fname $emp_lname") println("Employee Id: $emp_id") println("Employee Salary: $emp_salary") } fun main(args: Array<String>) { //calling employee() with named arguments to use default values //emp_id,emp_salary must match with the name of formal arguments employee(emp_id = 3, emp_salary = 50000) }
Output:
Employee Name: Arun Verma Employee Id: 3 Employee Salary: 50000
Example explained:
- employee() definition, accepts four parameters ie. emp_fname, emp_lname, emp_id and emp_salary in which emp_fname and emp_lname are default arguments.
- The default values (emp_fname, emp_lname) can only be used by calling employee() with named arguments (emp_id, emp_salary).
Example: Named Argument
//employee() definition fun employee(emp_fname: String = "Arun", emp_lname: String = "Verma", emp_id: Int = 3, emp_salary: Long = 45000) { println("Employee Name: $emp_fname $emp_lname") println("Employee Id: $emp_id") println("Employee Salary: $emp_salary") } fun main(args: Array<String>) { //calling employee() with named arguments //emp_id,emp_salary must match with the name of formal arguments employee( emp_fname = "Mohit", emp_lname = "Singh", emp_id = 5, emp_salary = 23000 ) }
Output:
Employee Name: Mohit Singh Employee Id: 5 Employee Salary: 23000
Example explained:
- employee() definition, accepts four default arguments ie. emp_id, emp_fname, emp_lname and emp_salary.
- Values passed during the employee() call, will overwrite the default values for the function parameters.
- Hence, it prints only the values passed during the function call.