WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Functions


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) {
   println(MyFunction("Welookups.com"))
}
fun MyFunction(x: String): String {
   var c:String  = "Hello!! Welcome To ---"
   return (c+x)
}
output Hello!! Welcome To ---Welookups.com
The function should be declared as follows −

fun (:):

Lambda Function

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

In Kotlin, you can declare your lambda and pass that lambda to a function.


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

Inline Function

Now, we can pass a lambda to another function to get our output which makes the calling function an inline function.

fun main(args: Array) {
   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("Ohhh!!!")
   action(a)// call to lambda function
}
Heyyy!!!Welookups.com