Wednesday 31 July 2019

Kotlin : Default and Named Argument (Post 5)

Hi,

In previous post , I explained about functions . In this post , I will discuss more about arguments in Kotlin .

In Kotlin , we can assign default value of the parameter in function definition. If any value is pass at the time of function calling , that passed value is used otherwise the default value is used.

Let's take a look for better understanding:


Method Body :

fun run(num:Int= 5, latter: Char ='x'){
Log.d(TAG,"parameter in function definition $num and $latter")
}


Case : I 

calling the function like :
setContentView(R.layout.activity_main)
run()

Output:
parameter in function definition 5 and x

Explanation: As we can see in function definition , by default value will be printed.
------------------------------------------------------------------

Case :II 


calling the function like : 
setContentView(R.layout.activity_main)
run(3, 'T')

Output:
parameter in function definition 3 and T

Explanation: As we can see in function definition , new passed value will override the by default values , hence the value has been changed.
------------------------------------------------------------------

Case :III 

calling the function like : 
setContentView(R.layout.activity_main)
run(3)

Output:
parameter in function definition 3 and x

Explanation: As we can see in function definition ,the first parameter with new int value will be override the default one and for second parameter , as nothing has been passed so second value will print the default one.
-----------------------------------------------------------------

Case :IV 

calling the function like : 
setContentView(R.layout.activity_main)
run('b')

Output:
Compile Time Error: The character literal does not conform to the expected type Int

Explanation: As we can see in function definition ,the first parameter is defined as integer value , so for 'b',compiler will take as a value for first parameter . But passed value type does not match with the first parameter in function definition. Hence compile time error will occur.
----------------------------------------------------------------------------------------------------------------------

Case :V 


setContentView(R.layout.activity_main)
run()

calling the function like : 
setContentView(R.layout.activity_main)
run(latter='b')

Output:
parameter in function definition 5 and b

Explanation: As we can see that here , I am passing name of that parameter name , to assign the value , so compiler will take as a second parameter value, from the calling statement because in this case name of parameter has also been mentioned. So in this way it will show that output. This scenario will called as Named Argument.
-----------------------------------------------------------------

Case :VI 

calling the function like :
setContentView(R.layout.activity_main)
run()

And methods are defined like this: 
fun run(num:Int= 5, latter: Char ='x'){
Log.d(TAG,"parameter in function definition $num and $latter")
}

fun run(){
Log.d(TAG,"function with no parameter")
}

Output:
function with no parameter

Explanation: As we can see there are two methods has been defined so , when we call the method without parameter , so it will call the second function , which doesn't have any parameter defined in function definition. If that function is not defined there , then the first function will get the call.
-----------------------------------------------------------------------------------------------------------------

That's it , will be back later with some new concepts in Kotlin, till then happy coding :) 

No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...