👨‍💻Kotlin Lambda Expressions + Kotlin Anonymous Functions = POWER

Hi! Do you know exactly the use of Kotlin Lambda Expressions and Kotlin Anonymous Functions? 🕵️‍♀️

Hüseyin Özkoç
Huawei Developers
3 min readApr 19, 2023

--

Kotlin Lambda Expressions + Kotlin Anonymous Functions = POWER

Introduction

Hello, Dear Kotlin lovers! Welcome to my new article. Today I am going to talk about Lambda Expressions and Anonymous Functions in Kotlin.

First of all, In Kotlin functions are “First Class Citizen”. Thus, they can be assigned as values to variables, given as parameters to other functions, or return values of functions. Likewise, there are 2 distinctive ways to assign a function to a variable(2 distinctive function literal) in Kotlin.

1-Lambda Expression

2-Anonymous Function

Now, we will look at these topics with their explanations and examples.

Lambda Expression

A lambda expression in Kotlin is a concise, unnamed function enclosed in braces, used for defining code blocks that can be passed as values or stored as variables.

Basic Usage of Lambda expression
Basic Usage of Lambda expression

Moreover, Lambda Expressions can take arguments and return values. Now, let’s examine the syntax.

Lambda Expressions can take arguments and return values.
Lambda Expressions can take arguments and return values.

In addition, we can use shorter syntax when creating Lambda expressions.

We can use shorter syntax when creating Lambda expressions.

Finally, there are four function types, varying based on parameters and return types.

There are four function types, varying based on parameters and return types.

BONUS: A variable isn’t always necessary, as lambda expressions can be used directly.

A variable isn’t always necessary, as lambda expressions can be used directly.
The output of the Bonus code.

Anonymous Function

In Kotlin, anonymous functions and lambda expressions are both unnamed functions that can be passed as values. However, anonymous functions use the ‘fun’ keyword, while lambda expressions are defined with curly braces {}.

Basic usage of Anonymous Function

In addition, we can use shorter syntax when creating Anonymous Functions.

Shorter syntax when creating Anonymous Functions.

Finally, Let’s explore various anonymous function formats, depending on the parameters and return type.

Various anonymous function formats, depending on the parameters and return type.
The output of Various anonymous function formats.

Conclusion

As a result, Kotlin functions are considered “First Class Citizen” which means they can be assigned to variables, passed as parameters to other functions, or returned as function values. There are two ways to assign a function to a variable in Kotlin, one being through a lambda expression which is a concise, unnamed function enclosed in braces. Both anonymous functions and lambda expressions are unnamed functions that can be passed as values, however, anonymous functions use the ‘fun’ keyword, and lambda expressions are defined with curly braces {}. Overall, these features in Kotlin allow for more flexible and concise coding practices.

References

--

--