Kotlin Higher Order Function and Lambda - #11 Kotlin Android Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will learn about higher order functions and lambdas in kotlin functions are first class means we can store functions in variables we can pass functions as arguments to other functions and we can also return function from a function this feature is very important and useful while working with callbacks and threads we will learn the details in coming videos but first let's understand how we define a higher order function a function is called a higher order function when it is accepting an argument that is a function or it is returning a function or it is doing both accepting a function as an argument and returning function as the return type sounds confusing right let's see an example to understand for example i will take the last function that we had created in the previous video so i will write fun roll dice now to this function i will pass some parameters so the first parameter that i will pass is the range so we have end range the next parameter is the times that how many times i want to roll the dice so we have end now the last parameter that i will define will be a function so what i will do is i will write here callback this is just a variable name so even when you are defining a function as a parameter you will write a normal variable name you put colon and then in place of the type you define a function syntax now to define the function syntax here you need to use the parentheses first now inside this parenthesis you can put all the parameters that are required now if you do not have any parameter you can leave the parenthesis empty now after defining the parameters you need to define the return type for your function and to define the return type first you need to use this arrow operator and then after the arrow operator you can define the return type for example you can define end float or basically any return type but for this example i do not need any return type but you have to define or write a return type and when you don't have any return type to define you can write unit as the return type it means this function do not have any return type now for this example i also need a parameter for my callback and for the parameter i will define an end that is the generated random number so we have the rolldice function ready now inside the function we will generate a random number between the given range and we will generate the number given times so to generate the number given times we can use loop so here i will write for i n 0 until times this basically means 0 to less than time now inside this loop we will generate the random number so to generate the random number we can create a val then i will name it result you can name it anything and then i will generate the random number between the given range so i will write range dot random like this [Music] now i will use this function to send a call back to the calling place so we can simply call this function because this callback is a function and we can call it as we call any normal function so we will call the callback and callback accepts an integer that in this case is the generated random number so i will pass the result because this is what i wanted to do now let's call the rolldice function inside our main function so we will call roll dice we will pass the range let's say one two six and i want to generate four random numbers and for the callback we need to pass a function and whenever we need to pass a function as an argument we define the function as a block of curly braces and this is called lambda now at the starting just after the opening curly braces we define all the parameters that are required for this function now in our case we have only one parameter and when you have only a single parameter in your argument function it is not necessary to define it when you are calling the function and passing the function because as you can see in the hand it is using it because we only have a single parameter but for multiple parameters you need to define all the parameters when you are calling the function and passing a lambda so this is called a lambda and it is basically a function that we are passing to this function because the last parameter of this function accepts a function sounds very confusing right but it is very useful and very important feature so it is not necessary to define the parameter when we have only single argument but if you want you can define it so i will define it like this result and then arrow operator and this is how we define parameters inside the lambda now here we can use the result so i will write println and i will pass result now if i run the code i will get four random values you can see we have four random values another important thing is when you have the function argument as the last argument of the function for this example the callback is the last argument or last parameter for this rule lies function and if this is the case we can put this lambda outside the parenthesis and this is called trailing lambda and it is very cool and handy in terms of writing the code now if i run the code now i will get the same output you can see we are getting four random numbers now you might be thinking that what is the use of this thing we can directly generate the random numbers inside the rolldice function what is the point of passing a function as an argument but trust me it is very handy i will give you an example let's say this function is doing some heavy work for example this function is fetching some information from a network or doing something that takes a lot of time and that is why you are doing that task in a different thread now to get the callback from this function to your calling function this lambda is very important i will write an example for you to demonstrate this thing let's create one more function so i will create one more function that is again roll dice and this time i will accept just a lambda so i will write here the same thing actually i will write i will write callback then we have result as integer and return type as unit now inside this function we have a return value so let's say we will return dice road and as this function is returning a string we will define the return type as string now when we call this function we will get this value i will show you we have print or let's say val result equals to roll ties like this and we can write println result so if i run the code now i will get dice rolled you can see we are getting the output but what if before returning this value we are doing something in a different thread for example i will create the thread but don't worry if you don't know anything about thread because we will cover it in detail in coming videos so for this example i will write a thread here and just to mimic that this operation is taking a long time i will write here thread dot sleep and let's say i will wait for 3 seconds and after this wait time i need to send a call back to this place that is my calling place so here i can send the call callback using this callback function that is an argument for my rolldice function so i can simply write here callback and then function sign like this and i can pass any value let's say 4 and here i will get the callback and i can print the value that is 4 in this case also we have only a single parameter so that is why it is not necessary to define it here and we can use this it keyword so let's run the code now you can see we got this thing dice rolled and after three second we got this four so we are getting a callback using this function that we defined in the argument of our rolldice function and another important thing is we can also assign null to our functions and argument so let's say i want to define null as the default value for my callback so what i can do is i can wrap it inside a parenthesis i can make it nullable using question mark and i can put the default value as null now it is not necessary to pass this trailing lambda here this thing is called trailing lambda so we can simply call roll dice and we do not need to pass this call back because the default value is null now the question is if the value is null how we can call the callback function so if it is wrapped like this and a default value is assigned that is null what we can do is we can use the invoke like this so to call this callback we can use invoke and we can pass the parameter as this callback is nullable we also need to put the question mark to make it null safe and now we are doing the same thing so if we will pass the callback we will get the callback if we will not pass the callback we won't get a call back now if i run the code i will get just this dice rolled as the output because we are not passing any callback you can see here and we will not get the callback because it is null let me show you you can see we are just getting dice rule but if i pass a callback like this and i can print it like this i will get the callback as well you can see we are getting four so this was higher order functions and lambdas a very important feature of cotton and you will use it many times while building android applications so if you have any questions please leave your comments below and that's all for this video i will see you in the next video thanks for watching you
Info
Channel: Simplified Coding
Views: 7,301
Rating: 4.8217821 out of 5
Keywords: Kotlin lambda, kotlin higher order function, kotlin function reference, kotlin callback function, kotlin functional programming, kotlin tutorial, learn kotlin, kotlin android tutorial for beginners
Id: ZZuPgOhgQHc
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Sun Nov 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.