Kotlin Functions With Examples

  • 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 create functions that are self-contained.
  • There are predefined functions that come with the Kotlin library but we can define our own functions as well. We have been using the println function - this helps in printing the output to the console. We just call this with the input and it prints onto the console.
Let's create functions - 

Using a function is a 2-step process - first, we define the function and then we call it. 

  fun add(a: Int, b: Int) : Int
    val total = a + b
    return total
Explanation - 
  • We have defined a function add here that takes 2 integer inputs a & b. fun stands for function and inputs are defined inside round brackets ( ).
  • Set of statements we want to execute are surrounded by 2 curly braces { }. All the logic goes inside this block of code. Here we just did the addition of 2 integers that are provided as inputs.
  • Once the processing part is done, we return the output using the return keyword i.e. return <whatever we want to return>
  • We need to also define the data type of the returning value. Since the sum of 2 integers is also an integer, we define the data type right after the input parameters i.e.: Int

This is how we define a function. First part of defining a function is done. Now let's call it and see how to pass inputs and get the output from this function.

  fun main() 
   	val result = add(3, 4)

  fun add(a: Int, b: Int) : Int
   	val total = a + b
   	return total
  • Calling a function is easy - just write functionName(comma separated inputs) - this is how you call a function. If the function returns any output, you can store it in a variable as shown above.
  • Output of the function is stored in a variable result. Data type of the variable must match with the return type of the function. Since Kotlin can infer the data type due to Type Inferencing, we have not defined the type explicitly.
  • In computer science terminology, inputs passed to the functions are known as Arguments and variables defined inside the function are knows as Parameters. So we pass arguments to the function which gets mapped to the parameters. This mapping is 1:1. i.e. first arguments maps to first parameter, second argument maps to second parameter and so on.


That's it for this article. For more articles on Kotlin Tutorials For Beginner Series - refer to this.

Happy Learning !! Cheers From CheezyCode.


Popular posts from this blog

Polymorphism in Kotlin With Example

Create Android Apps - Getting Started