A function is not a new concept. In this tutorial, we will learn about the Kotlin function with examples.

Functions are a core part of any modern programming languages. Functions help us to manage the large piece of code into small chunks. Easy to update and maintain the existing code. Help us to add new features very quickly.

Let’s consider we are developing a Math library. We want to add the basic mathematics features like addition, subtraction and so on. Should we divided the code into small separate functions or put everything in the main function. Of course it is better to create separate functions. If somehow there is any problem in addition we just have to adjust the addition function.

Function helps us to remove the repetition from the code. Well, this lesson is all about Kotlin Functions.

Type of Functions

There are two types of functions.

Built In function, functions that are included in the standard library. They help us to improve the programming experience.

User-defined functions. Functions developed by a user (programmer).

Kotlin Standard Library Function

These are built-in functions. We use print() a lot and that is a Kotlin Standard Library Function. In a small piece of code, we use a lot of built-in or standard library functions.

fun main(args: Array<String>) { val num = -12 println("The absolute value of $num is ${Math.abs(num)}") }

We have used a built-in function and that helps us to calculate the absolute value. println is also a Kotlin library function in this example. Result of this code.

The absolute value of -12 is 12

User Defined Functions

Now we already know what is a user defined function. A function that is defined by a user is a user defined function.

fun is a keyword that is used to define a function. After fun keyword writes the function name. Then use parenthesis ( ) . Function parameters are defined using Pascal notation i.e name:type . A function may or may not have parameters. It depends on function requirements. Next, define the return type using : . If a function is not returning anything then there is no need to define the return type. The last step is to write curly brackets { }

NOTE: In Koltin if a function is not returning anything it’s return type is Unit. But it’s not necessary to write it.

Let me show you with the help of an example.

Kotlin Function Example

fun evenOdd(value: Int) { if (value % 2 == 0) println("number is even") else print("number is odd") }

Let me break down it for you.

fun is keyword

evenOdd is the function name

value is the function parameter of type Int

there is no return type.

To call this function we have to write evenOdd in the main function and pass a Int parameter.

Let me show you the complete example.

fun main(args: Array<String>) { val num = 12 evenOdd(num) } fun evenOdd(value: Int) { if (value % 2 == 0) println("number is even") else print("number is odd") }

The result is.

number is even

What if we want to return some value from the function. It’s simple then we have to define the return type. Here we go.

fun main(args: Array<String>) { val num = 12 if (isPositive(num)) { println("The given number is a positive number") evenOdd(num) } } fun isPositive(value: Int): Boolean { return value > 0 } fun evenOdd(value: Int) { if (value % 2 == 0) println("number is even") else print("number is odd") }

Now in isPositive function we are checking if the given input is positive or not. It is returning a Boolean value. So we have defined Boolean as return type. The output is as expected.

The given number is a positive number number is even

So far we are using only one parameter as an example. What if we want to pass more than one paremeter

fun main(args: Array<String>) { val a = 12 val b = 10 sumNumbers(a, b) } fun sumNumbers(value: Int, value2: Int) { println("The sum of $value and $value2 is ${value + value2}") }

a is passed to value and b is passed to value2 . By the way, this is ${value + value2} string interpolation in Kotlin

This is just an introduction to Kotlin function. We did some function examples but in Kotlin there are so many kinds of functions. In the coming tutorials, we will cover these functions.

All these functions are based on the very basic concept that we just have studied.