dir.by  
  Search  
Programming, development, testing
Kotlin
Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };
  Looked at 1085 times    
 Lambda function in Kotlin 
last updated: 2 May 2024
Lambda function denoted by ->
Lambda function is a shortened version of the unnamed function.
The Lambda function is described as follows:
Example 1. no input parameters, returns nothing
Lambda Declaration
// no input parameters, returns nothing
// Unit is an empty type
() -> Unit


Lambda Body
// function displays Hello
{ -> println("Hello") }
Full example:
  Kotlin  
// Create the variable myFunc1: this is a function with no input parameters, returns nothing
var myFunc1 : () -> Unit;

// For variable myFunc1 set body of the function
myFunc1 = { -> println("Hello") };

// call the lambda function
myFunc1();
// We'll see on the screen Hello
 
 
Example 2. No input parameters, returns Int
Lambda Declaration
// no input parameters, returns Int
() -> Int


Lambda Body
// function only returns 30
{ -> 30 }
Full example:
  Kotlin  
// Create a variable myFunc1: this is a function with no input parameters, returns Int
var myFunc1 : () -> Int;

// For variable myFunc1 set body of the function
myFunc1 = { -> 30 };

// call the lambda function
var result = myFunc1();
// result = 30
 
 
Example 3. With 2 input parameters, returns Int
Lambda Declaration
// with 2 input parameters, returns Int
(a:Int, b:Int) -> Int


Lambda Body
// function calculate sum of 2 input parameters and return sum
{ p1, p2 -> p1 + p2 }
Full example:
  Kotlin  
// Create a variable myFunc1: this is a function with 2 input parameters, returns Int
var myFunc1 : (a:Int, b:Int) -> Int;

// For variable myFunc1 set body of the function
myFunc1 = { p1, p2 -> p1 + p2 };

// call the lambda function
var result = myFunc1(5, 21);
// result = 26
Writing a program
Step 1. Creating a new project
Step 2. Let's change the code in the file Main.kt
  Kotlin  
fun main()
{
     var myFunc1 : (a:Int, b:Int) -> Int;

     myFunc1 = { p1, p2 -> p1 + p2 };

     var result = myFunc1(5, 21);
     println(result)
}
Step 3. Launching the project
See the result 26 at the bottom of the screen
 
← Previous topic
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
 
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is Kotlin?
Why is IntelliJ IDEA the most popular development environment for Kotlin?
Download and install IntelliJ IDEA to explore Kotlin
Create a new project in IntelliJ IDEA for study Kotlin
Int, Float, Boolean, Char ...
Integer numbers in Kotlin are: Byte, UByte, Short, UShort, Int, UInt, Long, ULong
Decimal numbers in Kotlin are: Float, Double
A flag with values of true or false in Kotlin is: Boolean
The symbol in Kotlin is: Char
Convert number to text in Kotlin | Int → String
String
The string, the text in Kotlin is: String
Interpolate strings in Kotlin. Example: val address:String = "${street}, ${country}"
What is the difference between String and StringBuilder ?
Enum
What is an enumeration (enum) in Kotlin ?
How do I find enum by value in Kotlin ?
Class
What is class in Kotlin? Example: class MyBook { ... }
lateinit is a late initialization for the class field | Kotlin
class that inherits from interface in Kotlin | Example: class MyBook : IBook { ...}
Unnamed class that inherits from interface in Kotlin | Example: val book1 = object : IBook { ...}
Generic, template class in Kotlin | Example: class MyBook<T> { ... }
Collections and Arrays
Create collections list, set, map and an array array in Kotlin
null
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
Lambda function
Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика