Posts

Kotlin Function Types With Examples

Image
  In Kotlin, functions are treated as values. Just like your integer values or strings - you can assign functions to a variable. Kotlin is considered as First-Class Function Language i.e. functions are treated as values. You can store them in a variable, pass them as parameters and return a function from a function.  Function Type Since functions are just like other values, there is a type that is associated with the functions. That type is known as Function Type. Let's look at some examples -  1. (Double, Double) -> Double This is how we define a function type. If a variable has this data type then it can take any function which takes 2 doubles as input parameters and returns the output as Double. Function signature contains 2 things - What is the type of input parameters and what is the return type of the function. So when you define a function type you need to mention these 2 things. Before the arrow ( ->)  you define the types of input parameters i.e. Double, Double in th

Kotlin Function Variations With Examples

Image
In our previous post, we learned about functions. In Kotlin, we can define a function in different ways. Let's learn about these variations one by one.  Single Line Function If there is only one expression, we can shorten the function. Refer to the snippet below -  fun add(a: Int, b: Int) : Int { val total = a + b return total } // Single Line Function fun add(a: Int, b: Int) : Int = a + b //Kotlin can infer the return type fun add(a: Int, b: Int) = a + b Unit Data Type When a function does not return any value - the return data type, in this case, is Unit. You can explicitly define a function with a return type as Unit but Kotlin is smart enough to inference. fun printMessage(msg : String) : Unit { println("Hi ! " + msg) } // Kotlin can infer the return value in this case fun printMessage(msg : String) { println("Hi ! " + msg) } In other programming languages like Java or C#, when the function does not return any value it is

Kotlin Functions With Examples

Image
Complex application has thousands of lines of code. Writing them inside the main function is certainly not a good idea.  We split this code into separate and manageable chunks - each chunk with its dedicated functionality. This chunk is known as Function. In simple words - a function is a block of code that performs a specific task or functionality.  Now whenever we need this functionality somewhere in our code, we call this function. This function can take inputs and based on the inputs, it performs some actions and provides the output. This prevents code duplication and helps in code reuse. You can just define a function and call it whenever you need that particular functionality. Let's take one example -  Here you can see that ATM Machine acts as a function i.e. a standalone unit that takes input and provides you the output. Once you provide the card information, pin, and amount you want - it does some processing and gives you the required money. Similarly, in programming, we cr

Kotlin For Loop With Examples

Image
Just like while loop, we have a different looping syntax in Kotlin - For Loops. For loops are used to execute the statements a certain number of times. With Kotlin Range, for loop becomes more powerful. Let's look at some examples -  for(i in 1..5) { println("Hello CheezyCode") } Explanation - This loop will print Hello CheezyCode 5 times. 1..5 is a concept of range in Kotlin. This for loop will start from 1 and ends at 5. After every iteration, the value of i is incremented by 1.  Using step in for Loop You can increment the step count by using the step keyword followed by the number inside for loop i.e. This step value is used for incrementing the value of a counter variable. After every iteration, the value will be incremented by the step. Refer to the code snippet below -  for(i in 1..5 step 2) { println("Hello CheezyCode ${i}") } //Output - //Hello CheezyCode 1 //Hello CheezyCode 3 //Hello CheezyCode 5 Explanation - Here we have mentioned step value

Kotlin while Loop with Examples

Image
We use loops in our real life too. Let's take one example - Say I have a bag of balls. Balls are either red color or brown color. We need to distribute these balls into separate buckets. To do this what we generally do -  1. We pick one ball from the bag. 2. Check its color. 3. If it is red, we put it in the red bucket or if it is brown, we put it in the brown bucket. 4. We do this until the bag is empty. So we want computers to do the same for us. This is where Loops come into the picture. We keep executing the instructions until the condition is false. While Loop Kotlin has different variations of the loop. We will start with the while loop in Kotlin first.       while(booleanCondition)      { //block of code will be executed until the condition becomes false } Let's write a program - We need to print a table of a given number.  fun main() { val num = 2 var index = 1 while(index < 11) { println(num * index) i

Kotlin When Statement & Expression

Image
Kotlin When Statement Kotlin's when is another way of writing conditional statements in Kotlin. When you have multiple conditions, writing it using an if-else statement becomes less readable. To improve the readability, you can use the when statement . Let's take some examples -       val animal = "Dog" if(animal == "Cat") { println("Animal is Cat") } else if(animal == "Dog") { println("Animal is Dog") } else if(animal == "Horse") { println("Animal is Horse") } else { println("Animal Not Found") } Although this code works but having multiple conditions with multiple statements inside every block becomes cumbersome to understand later. We need more readable code. To improve this simple program we can make use of the  when statement       val animal = "Dog"      when(animal) { "Cat" -> println

Kotlin If-Else Expression and Statement

Image
If-Else Statement Humans have decision-making ability - they can take decisions based on the conditions. For e.g, if it is raining outside then it is better to take an umbrella. We want computers to make decisions based on the conditions and inputs we provide. This is where if-else comes into the picture.  This is a control flow statement i.e. it decides the flow of the code based on the conditions. These are also known as conditional statements. In simple words, we say -                 Me: Hey Computer!                Computer: Yes.                Me: Can you check if this Condition is true, if yes then do X thing else do Y.                Computer: I am a good servant but please give proper instructions. To write this type of code in Kotlin - we use an if-else statement. Let's see some examples -  if(booleanCondition) { // this code executes if the condition is true } else { // this code executes if the condition is false } Boolean can be