Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Amandeep Singh
Amandeep Singh

Posted on

     

Higher-order functions and lambdas in Kotlin

Functions in Kotlin By Amandeep

Higher-Order Functions:

Functions that return a function as their result, and function types which enable you to define functions as values. Some programming languages are purely functional, meaning the entire application is structured in this
style.
While Kotlin is not a pure functional programming language, it does provide many functional tools to allow developers to express logic in a functional paradigm. Here, we write our code-part after-> sign where type annotation is optional.
We must explicitly declare the type of our lambda expression. If lambda returns no value then we can use Unit
pattern :(Input) -> Output

But to understand Higher-Order functions with examples we need to learn about lambda expressions.

Lambda Expression:

A lambda expression is always surrounded by curly braces{} and arguments are declared inside the curly braces.
Syntax of lambda functions:

val lambda_name : Data_type = { variable -> body_of_function }
Enter fullscreen modeExit fullscreen mode

It will be easy to understand with an example:
We can use lambda functions to simply add to numbers 'a' and 'b'.

example with type annotation:

val add = { a: Int, b: Int ->     val num = a + b }
Enter fullscreen modeExit fullscreen mode

example without type annotation:

val add:(Int,Int)-> Int  = { a , b ->        val num = a + b}
Enter fullscreen modeExit fullscreen mode

Adding two numbers using lambda-functions:

val add = { a: Int, b: Int ->    val num = a + b    num.toString()     //convert Integer to String}fun main() {    val result = add(2,3)    println("The sum of two numbers is: $result")}
Enter fullscreen modeExit fullscreen mode

Output:

The sum of two numbers is: 5
Enter fullscreen modeExit fullscreen mode

We can pass a lambda expression as a parameter to Higher-Order Function. Now we will go throughHigher-Order functions:

var greeting = {println("Hello, My name is Amandeep Singh") }      // using higher-order functionfun higherfunction( greet: () -> Unit ) {     // use of lambda as a parameter    greet()                               //invokes lambda function}fun main() {     //call higher-order function    higherfunction(greeting)                 // passing lambda as parameter}
Enter fullscreen modeExit fullscreen mode

Output:

Hello, My name is Amandeep Singh
Enter fullscreen modeExit fullscreen mode

That's it for lambda and Higher-Order functions. Now you have a tight grip over them.

If you have any doubt or suggestion you can share it in the discussion section.
Wanna connect? connect with me onLinkedIn

Thank You Image

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

CS Student sharing his learningsML | MLOps | Android Dev | Data Science
  • Location
    Kolkata, India
  • Education
    JIS University
  • Joined

More fromAmandeep Singh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp