#24 Kotlin Programming Tutorial - Higher Order Function and Lambda

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys this is Bilal Khan from simplified coding we are learning Catalan programming language and in this video we will learn about higher order function and lambdas so first start with higher order functions so it is a function which takes function as a parameter and returns a function or it can do both sounds confusing right but it is actually very easy just imagine in a function we are passing a function as a parameter or the argument to the function so this type of function which takes function as a parameter in Catalan is called higher-order functions now come to lambda the very simple definition of lambdas is it is a function with no name or it is also called function literals now I will show you by creating a higher-order function so in this video we will see how we can pass function as a parameter to a function and in the next video we are going to see about returning functions from function or then and then you can do the both you can do both operations with your higher-order function now let's start so let me delete it so as I told you a function which takes function as a parameter is called higher-order functions so let's create a function so in this case I am creating a very simple function let say it make my hero now this function will take two strength parameters so we have real name which is strength zero name which is also a string and then the third parameter which is our function so I am naming it make and it is a function so first we will define the parameters of this function so this function will take two strings we do not need to define the names of the parameters here so we simply defines the datatype so we are passing two strengths to this function now after the arrow operator we will define the return type so in my example the return type is also a string but it can be anything according to your example or your program so this is a higher-order function now when we will call this function we will pass the third parameter as a function now to pass a function here we do not define a function so the function that is passed here has no name and this function is called a lambda expression so a lambda expression is a function with no name and it is also defined inside curly braces now before the error operator we define parameters and after the arrow operator we define the body of the lambda expression so this is how we define a lambda expression now first I will define a lambda expression here to pass as a parameter as a third parameter of this function to this function so at the point let us define a lambda expression so first we will write Val and let's say it make now it does not necessary that it both should be same and T's should be different as well I am just naming it mate so it makes some sense so this is a lambda expression so it will take some parameters and as we have already defined it will take two strings as a parameter so with the right strength saying now it will also return a strength so this is our lambda and what it will do it will take two string and which is really and hero name and remember lambda expression is always defined inside curly braces so we have real name and we have hero name and after the arrow we will define the body so it will simply return a string which is real name is now he donut so what it will return real name whatever the name we will pass is now and whatever the hero name we will pass so this is a very simple lambda expression now we can use this lambda while calling this function so let's complete this function so inside this function we will write Val result equals to and we will call this function so the right make and we will pass real name and hero name so what will happen when this line is executed when this line is executed actually this method this lambda expression is called which is passed here so right now we haven't called the method so you will understand it better then we will call the method so what will happen this make expression will be advocated and it will return the string real name is hero name and it will be stored inside result so we can simply print the result that's it now we can call this function so simply call it make my hero and pass a real name let's say Tony Stark and hero name which is Iron Man and then our lambda expression this one so you can see it is correct now let's compile it and execute it you can see we are getting Tony Stark is now higher now so what's happening here and these two parameters are getting passed here in this real name and hero name and the make which is a lambda expression is passed in this third parameter so we have make this expression so here what is happening it is simply the Tony Stark is now he do name this is Iron Man so this thing is happening here and getting stored inside this result and we are printing the result so this is called a higher-order function and this is called a lambda expression so this is the first way method one of calling a higher-order function now let's see the second way so let me comment it now you can directly pass the lambda expression so what we will do we will again write Tony Stark or any other name that you like Iron Man and the third expression is a lambda expression so we can here right real name and hero name and then our body so we can copy this actually so copy it and paste it here now this is actually the same thing we did here so let me just show you by running the code and we will get the same output you can see Tony Stark is now on man now the third way is this was method two now let me show you one more way so comment it out and we can again call make my hero and then again Tony Stark Iron Man and then put the curly braces after this and put the same thing here now this is also the same we will get the same output you can see now put the same so what's important is these are the different ways of handling higher-order functions and lambdas but the important thing is here we are passing a function to this function so this is what is called a higher-order function in Kotlin so these are the three bays and there are more ways actually and we will cover it in further videos so this is it for this video guys if you liked it please like the video subscribe to my channel and spread this video on your social networks so thanks for watching and if you are having any confusions or queries or you want to gain any feedback you can leave your comments and we will meet in the next video goodbye [Music]
Info
Channel: Simplified Coding
Views: 5,768
Rating: 4.807229 out of 5
Keywords: kotlin tutorial, kotlin tutorial android, kotlin code examples, kotlin tutorial for beginners, kotlin for beginners, kotlin tutorial step by step, kotlin course, kotlin programming language tutorial, learn kotlin for android, the complete kotlin developer course, learn programming with kotlin, kotlin for android developers, higher order function kotlin, kotlin higher order functions, kotlin lambdas, lambda in kotlin, lambdas in kotlin
Id: _QqpI-2EtgU
Channel Id: undefined
Length: 9min 42sec (582 seconds)
Published: Sun Aug 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.