Kotlin Tutorial - Lambda Functions, Inline Functions, Higher Order Functions Extension Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to the five video series of court lengths and this is a video number three quarterly functions in this video we'll discuss about how to create different functions in courtrand what are in life functions how to use recursion using tail recursion feature of Cortland what are lambda functions and what are higher functions so let's jump into the code with my favorite IntelliJ IDE and let's try to create a basic simple function so a function and cortland is declared using the fun keyword so much fun right all right so let's create a function named is square and it will accept one input parameter let's call it X and it will be of a type integer and this function will return me an integer type of value so the return type for this function is integer so let's say its own statement and it will return X into X which is X square let's run this function by calling it in a would print aligned statement directly Square and we pass in a number let's say 2 and let's write something here square of 2 is equal to we write the dollar symbol and we also had expression here using the curly braces and we end our strength okay so let's run this program and the square of 2 is 4 good enough we can also skip this return statement and directly write the body is in the equals 2 method and this is this is going to work perfectly fine in cutlet so there's going to be no issues with that let's create one more function function say hello it will take a name of string type and it will print hello and the name of the person so let's call the function here Ritalin let's say the name is Jo let's see how did you know shortly but B call the method say hello and pass him Jo here okay let's run this and it says hello Joe okay cool so now we know how to greet functions this is the name of function here is the parameter and this is a parameter type which died Epistle of the parameter and you can also specify the return type of this function which is an integer here and in this case it is unit by default so in Java or C if you have already if you're already familiar with those languages we had a void return type for those methods but here we have unit so you can also choose to skip it it will work perfectly fine without there as well let's also see what are named arguments so let's call my function let's say say hello again and typing in named argument here name equals to and I can print a name here let's say well so thing over here in blue is a named argument so this works best if we have a lot of parameters for a function let's say I'm displaying display data and it has a lot of parameters for example that's name and the age and which class do you study in let's skipped class let's say the subject that you study all right I think this is good enough for now and it will display that hi my name is aim and my age is in age and a sturdy donor subject good so let's call this method from here ritalin dollar side in the newcomers display data and then okay let's start the expression here and expression display theta dot oh sorry display theta and we pass in the values here so here we can do something like this name is equal to let's say and then we can pass the so this is supposed to be a string no Sigma type and compassed age equal zero three seven and you can pass the subject study is computer science alright let's go and run this program so this is basically using named arguments and cotton hi my name's Corral and made it himself and actually computer science good so this is the way you can add named other words it works well if you have a lot of parameters it's helpful in defining which parameter should have which value alright so we can do one more thing here if the user does not specify an eight here giving it a default value of 12 so here I can choose to skip the age and I can say a default or parent so here I don't need to necessarily specify age if I do not specify the age it will by default be 12 so see hi my name is Karen and made a stroke so in that case the age will be okay let's go further and create another function well get some where arcs and this type of teacher there are there are sorry and we can type in a mess nope and this function will also return a type of teacher this function is give me the balance or you can say calculate the sum of values so this function will calculate the sum of values and what we can do here is create a variable named Sun right value 0 and then we can call for each property on the numbers and for each number we will let's call it it each number we'll add it into the Sun all right and now we will return the value of sum so let's call this method get some from our main method returned sum of 1 2 3 4 is symbol and recall get some and we passed all those values separated by comma so it will take a bunch of values just like an array and show me the result which is a sub so the summer point 2 3 & 4 is 10 4 plus 3 7 8 9 10 all right good enough so let's go further and talk about extension functions so what are extension functions in Kotlin let's create an extension function say for string dot greet hello and what will do is it will print a statement with hello and whatever string object we are passing it will print that so it will take a name and you can send treat hello to that person so let's call this method from here okay so let's say I have a variable named name equals 200 let's take any name we'll take my name Corral and then we call named Tod such the method reached hello so this method was earlier not there in the string class but we have sort of treated an extension function for this string class which can be called on any string object so let's run this program it will say hello current and we can change the value of this variable to anything whatever you like and it will use the value of that variable and display the van I love you all right so this is about extension functions they come in really handy are really useful feature in plain language so you can also go ahead and pass any parameter here you can pass in let's say the name of instead of a name let's say you pass a value a and you can also use to print that value and send the value here which can be the check one control-shift are running the program say hello one so you can use your parameters along with the object that was called but that is calling this method so you can do both of the things alright so let's go further to lambda functions oh the lambda functions are also called as an ornament phone no newest functions because it has no name so let's create a lambda function here so a lambda function is no name let me create a variable there which is some and initialize it with a lambda function so this is basically an empty lambda function here I'm going to create a parameter number one which is of type integer and here's a parameter 2 for that lambda function which is also of type integer now the body of the lambda function will be basically pairing these two values not one enough to and it will return that value inside some so let's go to the main method and print the sum here some o 10 and 20 years dollar symbol this um and passing the values 10 comma 20 okay so we need to have an expression here the curly braces for that and let's call this let's run this program let's say the sum of 10 and 20 years which is Ratanak from here the parameters for this lambda function are num1 and num2 which are both of integer type and they are returning this they are calculating this expression which is the body of the lambda function and we call the lambda function use Excel variable which is getting the value from there okay so this is for the lambda functions let go to a higher order function so a function that accepts or returns another function is called a higher order function so let's see what exactly does it mean here after it some example in the PPT a higher order function let's say we have a method function hello which accepts one parameter as name of datatype straight and it has some body and now we have another function f you and my function which also accepts one parameter of type string and it also accepts another parameter which accepts a function type value the first parameter accepts a string type value and the second parameter accepts a function type at so let me explain this string to you so this is the name of the parameter which is named in this case and in this case it is my function myfunc and then again we have a column now which means that it will tell me which type of value does it does this parameter take so it takes in a function which we know because we are using round brackets and it takes a function which also takes another parameter of type string and it returns unit so hello is the perfect example for this and we can pass in hello to this function so the way you can pass hello to my function is by using this memory reference operator you can use that for pass the method so let's go back to the code and use it so let me create a function for you let's call it demo it takes a string and it will do nothing else but just print hello and the string that's called name for now hello okay and let me create this is a normal function basically normal function and let me create a higher or a function now so let's call this function as my higher-order function alright so this takes a value of name and it also takes an input parameter as a function so let's call it my function for now and give it it give it the return type so it takes a return type of function which returns it takes a parameter of string and you dance your it so now let's give it give this method a body which will do nothing measure but it will call the function passed into this with the value of name all right so let's call this function now and the way we can call it is by writing calling this method and passing in let's say a name which is let's say can you name for now not I'm literally running out of names let's call it odd alright and now we pass in no function named demo like this by using a reference operator so we passing in this function inside this function like in this parameter or like this so let's run this and we can write a print statement here this is my higher-order function okay let's run this program now this is my higher function and this function basically demo is passed into it which is being called here so this is a example of a higher order function where it takes a parameter as an input another so we can say takes a function as another per meter all right so this is it for the higher-order functions let's jump onto inline functions now let's see what our inline functions so basically inline functions are so let's so let's talk about inline functions now so when we're using a higher-order function they impose certain kind of penalties for example each function is an object so let's open PPT for now and see an England function so here again we have a simple hello function which takes one parameter last name and it has some body with it and when we call that function hello from the main method it will basically create some kind of memory which will be like initialize an object so it's same as initializing object and what we call that function again hello it will again utilize some memory so I'm not saying this is exact two by two three bytes with just some random value it can be anything but when we call the main function it takes some value it is initializing that method as an object and when we call the hello method here it is again initializing it as an object here and when we call hello okay it is again initializing it as an object here which will take some memory so now let's take an example of an inline function let's call this let's make this function as inline and then what will happen is that when we call the hello function from main function and we run this program the main method will be the only one which will be initialized as an object and the main method will be modified so which means that the inline modifier will bring the functions to the call site and new object declaration will not happen this will save the memory and in some cases where we can run into a stack overflow error this will help to save that as well so let's go back to the code and try to create an inline function so there's one more thing associated with inline functions that we should avoid to create those methods as inline which have a large body we should only do it for functions which have a small body and also let's see something right now let's call this function as inline and see what happens so I'm getting a bit of a suggestion from quatrain compiler that expected performance impact from inlining is insignificant and in lighting works best for functions with parameters of functional types which means that inline modifier here is not of much value but if I use inline method inline modifier here for method which takes another method as an input parameter here makes actual sets so let's go run this program so in this case the my function will have be replaced by instead of calling this and declared memory it will actually be replaced by this line of code and whatever the value we are passing in the name so when we compile this code do two in line no object memory will mitigate for this method or it will bring all the code from the body of this method from here to here I hope this makes sense don't freak out if you are not able to understand this in one go just try and try to create your own inline method and you will have a better and Static fit alright so in life function is covered and there's also one more thing which is called as no inline modifier and I can choose this let's say I want to choose that this method should not be enlightened so I can add a no inline modifier here and in this case it doesn't make any sense because this is the only method which is as a parameter so if you are not implying that and they it's no sense in using the in like keyboard but if we do something like this it's copying this code and paste it here and we add another function here and let's call it let's take in the parameter here and let's call it my function one which accepts another but after spraying and it returns without unity and let's call it here again my function one with the C value NIEM so here let's call this method my another higher-order function and let's call it over here and passing some values we can pass in the name here we have to pass two methods here so we'll pass the demo method again so here in this case we have an inline modifier but let's say I want I don't want this method to be in light so I can add a modifier no inline to this which will make that only this method will be in light and this method will not be in light so at the time of compilation this will change to basically this statement name and this method will not be in life okay cool so I think this is it for this video if you have any questions please comment in the video below and all this board will be available to you in a github link in the description so that's good for you guys so if you have any suggestions please write it on the comment box below and we'll make some videos on that thank you and see you next time bye
Info
Channel: Team MAST
Views: 2,843
Rating: 4.6923075 out of 5
Keywords: kotlin, kotlin functions, higher order functions, lambda functions, inline functions, extension functions
Id: F8tLN524yk4
Channel Id: undefined
Length: 25min 48sec (1548 seconds)
Published: Thu Apr 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.