black friday sale

Flat 10% off on the purchase of any two apps or more. Use coupon code - FESTIVAL10

Grab It Now
Check Mobile App
Read now
Start a Project

How to pass function as parameter in Kotlin

Save

In this blog, we are going to see how kotlin lambda expression makes our code more expressive and concise. Recently, I’ve noticed that there are a few different ways to pass a function as a parameter. In this post I will use 2 functions: We have to declare the method, in such a way that it can receive the method as an argument. The most common and often more convenient (according to Kotlin documentation) is to pass a lambda expression.

1. Pass a lambda expression

In the above example, we have passed a lambda expression to calculateSum() method which will return the sum of these variables after execution of divide function. This is how we write lambda expression in a function:  Expression_Name(Param1,Param2,Param3)->RetrunType

2. Pass lambda expression directly without passing method

In this 2 example, we have lambda expression directly without passing method itself in the previous example we have passed the divide() function as the parameter. Regular functions receive only data parameters, whereas a function that receives another function as a parameter or returns a function, as a result, is called a Higher-order function. So, our functions that receive a lambda callback are basically higher-order functions.

 

Hope this blog will give you a basic idea of how we can pass lambda express or function to a method.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php

Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.

Osama
Talk to Sales

Global

Live Chat
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home