Tags

Merry Christmas Guys! The year 2018 is about to end. This have been a great year. Happy New Year 2019, May your new year be filled with Joy and Happiness. This will probably be the last post for this year 2018. Hope to see you all in the next year!

In today’s post i am gonna write about Function. If you have been programming in any of the language then you’ve been making Functions. Functions are the basic building blocks in programming. Let us discuss about the way to making Functions in the Kotlin.

First of all, let us see the basic syntax of the function in Kotlin.

fun getSum(a : Int, b: Int):Int {
	return (a+b)
}

The function above named getSum takes two parameter a of type Int and b of type Int which returns type Int.

return statement returns the value from the function. It is similar to the Java or other programming language.

Next, you can also take no parameters and return type in the functions which can be seen in the code below:

fun printString() {
	print("Hello!")
}

So, if there is no return type then the default return type is Unit which is similar to void in Java.

We can also write the single-expression function in the following way:

fun sum(a : Int, b : Int) : Int = a + b 

We can also define the function inside the function as follows:

fun greetName() {
	print("Hello! ")
	fun printName() {
		print("Sudeep")
	}
	printName()
}

Lambda

If you have ever done functional programming like Haskell then this might be easy for you to understand. Functions in Kotlin can be passed as a value to other function. Higher order function is the function that takes function as a parameter and returns a function. And the Lambdas are the anonymous function.

val greet : (String) -> Unit = {s:String -> print("Namesta! " + s)}
val name:String = "Sudeep"
greet(name)

Here we have a lambda greet which is define as a value and contains a function definition. Next, we pass the variable name to this lambda function.

It makes the code shorter and more readable.

Higher order functions

As we talked earlier on introduction, these are the functions which takes function as a parameter or return a function.

fun userName() : String = "Sudeep"
fun greetUser(body : () -> String) : String = "Hello! " + body()
print(greetUser { userName() })

Here, greetUser() is a function which takes another function userName() and returns a String. The function userName() returns Name of the user.

This is the simplest example of higher order function.

Inline function

We can pass the lambda function to another function which is inline function. It is similar to Higher order function.

fun main(args: Array<String>) {
   val lambdaFun:(String)->Unit  = { name :String->print(name)}
   val name :String = "Sudeep"
   greetUser(name , lambdaFun) 
}
fun greetUser(name :String, greet: (String)->Unit) {  
   print("Hello! ")
   greet(name)
}

Here, we pass the lambdaFun to another function greetUser.

That’s all folks for this post. In the next post we will discuss further about Kotlin.

Advertisements