Functions in Kotlin

Being a statically typed language, functions have an important role to play in Kotlin. You’re all familiar with functions of course, and functions in Kotlin are not so different from those in other languages. In Kotlin, we declare functions using the keyword “fun.” As with other object-oriented languages, you’ll also need a return type and an optional argument list.
Let’s not wait any time here and jump right into an example. Here, we’ll define a function called FirstFunction which we’ll call from main before passing an argument. Pretty basic stuff, so let’s go for it:
fun main(args: Array<String>) {
println(FirstFunction("DiscoverSDK.com"))
}
fun FirstFunction(x: String): String {
var c:String = "Hello and welcome to: "
return (c+x)
}
When we run this code, we receive the following output:
Hello and welcome to: DiscoverSDK.com
Notice the basic syntax used in the function:
fun <functionName>(<argument>:<argumentType>):<ReturnType>
OK, now let’s have a look at some different types of functions we can use in Kotlin
The Lambda Function in Kotlin
Lambda functions in Kotlin are high-level functions that are really handy in reducing the amount of boilerplate code we need to use by defining a function and declaring it. In Kotlin, we are free to declare our own lambda functions. Just declare your lambda and then pass it to the lambda function.
Here’s an example:
fun main(args: Array<String>) {
val firstlambda :(String)->Unit = {s:String->print(s)}
val v:String = "DiscoverSDK.com"
firstlambda(v)
}
In this little piece of code, we created our own lambda called “firstlambda” and then passed one variable to it. This variable is of the type String and has the value of “DiscoverSDK.com”.
No surprise, here is what the code returns:
DiscoverSDK.com
Inline Functions in Kotlin
In the last example, we saw a basic lambda function in Kotlin. Now, we can pass that lambda to a different function to get our output. This function that makes the call is an inline function.
Let’s have a look at an example:
fun main(args: Array<String>) {
val mylambda:(String)->Unit = {s:String->print(s)}
val v:String = "DiscoverSDK.com"
myFun(v,mylambda) //passing the lambda as a parameter of another function
}
fun myFun(a :String, action: (String)->Unit) { //passing the lambda
print("Hello ")
action(a)// call to lambda function
}
With our inline function, we passed our lambda as a parameter—pretty useful! Any function can be made to be inline—simply use the keywords “inline.”
Here is the output from our example”
Hello DiscoverSDK.com
That’s all for this time. In the next article, we’ll take a look at exception handling and more.
Previous Article: Delegation in Kotlin | Next Article: coming soon |
Recent Stories
Top DiscoverSDK Experts


Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
15 Comments
Great Information about KOTLIN. Technoelearn
Report spam or abuse?
Are You Sure?
Great post and thanks for sharing
web app development company
Report spam or abuse?
Are You Sure?
Wonderful article. It's very useful.
Hire skilled app developers from App Development Company to overcome the void. We unite with you to boost your productivity in the global industrsy.
Mobile app development company
https://bit.ly/3dLQQEf
Report spam or abuse?
Are You Sure?
Collaborate with skilled app developers at App Development Company to bridge the gap. We collaborate with you to boost your output in the global market. But in the soccer game, you may shoot penalties in whatever fantasy league you decide to participate penalty shooters 2 in a ferocious exchange, switch between the shooter and the goalie.
Report spam or abuse?
Are You Sure?
Looking for help with writing your thesis project? Turn to EssayService! Professional capstone project writing, high quality, meeting deadlines and personalized approach. Learn more and place your order here on capstone project writing service https://essayservice.com/capstone-project-writing-service!
Report spam or abuse?
Are You Sure?
Your Comment