Higher Order functions in Kotlin - Kotlin Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so in this video we are going to learn about higher order functions in cartridge what are higher order functions let's let's first understand that and then we'll understand how we write it so a higher order function is basically a function which takes function as a parameter or it returns a function so till now what we were doing is we were taking a variable of type string integer or any data type as a parameter and we were returning a certain data type as an output right so if i say we were writing a function add it used to take number one i'll just write it for short and it was returning like this right so here these two parameters are integer and this return value is also integer right but a higher order function would specifically take a function as a parameter or it would return a function so let's first declare a function here so i'll declare a function like fun i'll name it as do addition and let's say i'll create another function called add numbers now this takes two parameters again num1 of type integer and num2 of type integer and this returns num1 plus num2 so now what we are going to do is in this function we are actually going to return this add numbers function so for first so if you remember from the lambda video we were actually declaring a lambda expression like well sum then we are passing a colon and then we were creating the parameter type and then the return type and we're opening the brackets and here we were returning as integers right and here we were doing our task so what this lambda function does is it takes an integer parameter and it returns an integer so here in this function as well what we'll do is we'll actually return a lambda from do addition and this lambda expression so anything which we have written inside these two brackets is a function so what we'll do is we'll create something similar like this but here in place of a single inch i'll pass to it and in return what i'll do i'll pass add numbers so what we are doing here is we are creating a function do addition and we are returning a lambda here a lambda expression which takes two numbers of type in and the function returns a type integer so we already have that function here which we created as add numbers which took two numbers and returned the sum of it of type integer so then we return that specific function into addition now now to use this function edition what we'll do is we'll create a function of edition then we'll call do addition right oh what here has happened is here this addition is converted to a function and this do addition has been assigned to this and now if i want to print the sum i'll call addition function here and in this i can pass two values let's say two comma three and first let me run this to show you you would see five is coming up so basically what we did here was we first created a function do addition which returned a lambda function which took two parameters of type int and returned an integer then we created another function add numbers which was a traditional function which took two parameters and returned the sums then what we did as a next step was we returned that specific add numbers function in the do addition function and to use this do addition function what we did was we created val addition and we called to addition function so do addition took no parameters so that is the reason we didn't pass any parameters here then this addition got converted as a function and to get the sum now we used addition 2 comma 3. so we saw how to return a function from a function now let's see how we can pass a function as a parameter let me delete all of these and let's create a function called print my name so this is a typical function in kotlin now we'll pass a function as a parameter to print my name so here what we'll do is we'll actually pass a string as a parameter and that's to that parameters function and will return nothing so so i'll name the function as print and then i'll use a similar type how i used it for lambdas i'll use colon i'll open the bracket and i'll make a return type of unit so this print function takes nothing as a parameter and also returns nothing so that is the reason we used unit and i'll call the print function like this so this print is getting called here if you can see this and now to use print my name you could see this thing this lambda actually gets opened as a bracket that is why when i said anything inside this brackets is a function so this represents the print function and now if i print something here let's say i'll print him anshu and now if i run this you would see himanshu being printed up and in this if you see we are not getting it here the reason is we are not passing anything to the print function as a parameter and that is the reason we are not getting it here so now if i pass string here you would see it coming up and now the print requires a string as a parameter so now let me pass himanshu here and in place of human show here i'll pass it the value which i'm getting inside the curly braces so now let me run this you would see manchu being printed again we just called the print function then we passed himanch with the print statement here but in this case we are passing string as a parameters type and then we are returning nothing again so to explain your hypothetical concept how this actually looks like this print my name actually takes a function so the function looks like this and we do something inside that function so this is just for a hypothetical concept to explain you in a much better way how does it look when we call the print my name function which takes a function as a parameter so if you have to write in a traditional way this is how i would be writing it where i'm calling print my name function which takes a function so i have called a function again here as a parameter to print my name function so let me delete this and this is all about higher order functions in kotlin
Info
Channel: MindOrks
Views: 3,601
Rating: 4.6056337 out of 5
Keywords: MindOrks, Android, AndroidDev, Tutorial, Kotlin
Id: Q6f3r2ICWAM
Channel Id: undefined
Length: 6min 4sec (364 seconds)
Published: Thu Aug 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.