WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Functions


Kotlin is a statically typed language, hence, functions play a great role in it.

Like any other OOP, it also needs a return type and an option argument list.

we are defining a function called MyFunction and from the main function we are calling this function and passing some argument.
fun main(args: Array<String>) {
   println(MyFunction("welookups.com"))
}
fun MyFunction(x: String): String {
   var c:String  = "Hey!! Welcome To ---"
   return (c+x)
}
output

The above piece of code will yield the following output in the browser.

Hi!! Welcome To - Welookups.Com

Lambda Function

Lambda is a high level function that drastically reduces the boiler plate code while declaring a function and defining the same.

fun main(args: Array<String>) {
   val mylambda :(String)->Unit  = {s:String->print(s)}
   val v:String = "welookups.com"
   mylambda(v)
}

The above piece of code will yield the following output in the browser.

welookups.com

Inline Function

The above example shows the basic of the lambda expression that we can use in Kotlin application.

fun main(args: Array<String>) {
   val mylambda:(String)->Unit  = {s:String->print(s)}
   val v:String = "welookups.com"
   myFun(v,mylambda) //passing lambda as a parameter of another function 
}
fun myFun(a :String, action: (String)->Unit) { //passing lambda 
   print("Heyyy!!!")
   action(a)// call to lambda function
}