Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

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

Tuesday, 30 July 2019

Kotlin : Function (Post 4)

Hi All,

In previous post , I discussed about the control flow in Kotlin. Now continuing the basics in Kotlin , here in this post we will discuss about how functions are declared and called in this language.

=> Functions are used "fun" keyword to declare it.

=> Here I am showing how user define functions are defined in it.

Simple function example :


fun sayHello(){
Log.d(TAG,"Hello")
}

Output:
Hello
----------------------------------------------------------------

Parameterize Function and Return Value :


fun sum(num1 : Int , num2 : Int) :Int {
return num1+num2
}


calling the function : var num3 = sum(a,b)

Output:
Sum of two numbers:8
------------------------------------------------------------------

Named Parameters :

In kotlin we can have  the named parameter , let's take a look how we declare these type of functions:


  findVolumn(height =6, length =4 , width =5 )

And function body like :

  private fun findVolumn(length: Int, width: Int, height: Int) {

        println("Length is:"+length)
        println("Width is:"+width)
        println("Height is :"+height)
    }

Output:
Length is:4
Width is:5
Height is :6

Here , in named parameter we can see , that with the help of name of that parameter , whatever the order of the argument , it will use on the basis of its names. 

Benefit of this type of parameter is , with the help of parameters names , safer side function will handle the actual argument , so this will prevent any mistake by defining the values right & clear.

That's all for this post, I will update more types of functions in Kotlin in my future posts.








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