Default And Named Arguments

In most programming language, we must specify all the arguments that a function accepts while calling that function. In Kotlin, we have a feature using which we can get rid of this constraint. We can make parameter optional while calling the function. i.e. we may pass or not pass while an argument while calling function. 

Default Arguments

Arguments that are not required to specify explicitly while calling a function is called default arguments in Kotlin.


Syntax with an example

displayGreeting is a function that accepts two arguments (message and name). Note that we have initialised the variable name while defining the function. This is to ensure that if nothing is passed as second arguments while calling this function, it will be assumed that value of variable standard is “Guest” .
For example, If nothing is passed as second argument while calling function, value of standard would be “Guest”. So, argument standard is default argument in Kotlin.


Hello Inspire Coding , Welcome
Hello Inspire Coding , Nice to meet you!

All are valid calls of the function displayGreeting.
Note that in first case we have not passed any value for the second argument. It means the value of name will be “Guest”.
In the second case we have passed “Inspire Coding” as the value of argument name.

If we compare the displayGreeting () method with our main() method, then we will see how values passed to the function while calling any function, are assigned to formal parameters of the function.

In the first case displayGreeting("Inspire Coding") only “Inspire Coding” has been passed to the function as the argument name. The message is not necessery, because it has a default value.

In the second case displayGreeting("Inspire Coding", "Nice to meet you!") we have passed the second argument message also.


Hello Guest , Nice to meet you!
Hello Inspire Coding , Nice to meet you!
Hello Inspire Coding , Welcome
Note that both the formal arguments are default arguments.
There are three calls to the show function.
  • No arguments are passed: Call without passing any argument is valid because all the arguments of the function are default arguments. So, default value of the arguments will be printed out.
  • Partial arguments are passed: Only one parameter is passed. No problem. Value passed as parameter will be assigned to first formal argument (i.e. name) of the function. Second formal parameter (i.e. message) will have default value ie. “Welcome”.
  • All arguments are passed: Value of the actual parameters will be assigned to the corresponding formal parameters.

Named arguments

Arguments that are passed with name while calling a function are called named arguments in kotlin.


Name = Peter , gender = Male , age = 10

This key-value parameter will act as pre-assigned value for formal parameters of the function show. Here, key is gender in key-value. So, After function call, this key-name (i.e. gender) will be matched with formal parameters of function. When any parameter-name matches, the value of the key will be assigned to that parameter. Since we are passing the values of the function parameters using name of the arguments, It is called named argument or named parameter in Kotlin.

Since we are passing values as key-value form, Order of values, in which values are passed, does not matter. You can pass values in arbitrary order.


Name = Peter , gender = Male , age = 10

 

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *