‫Welcome back in Kotlin.
‫Anonymous functions are a way to define a function without giving it a name.
‫They are often used for creating small, one off functions that are passed as arguments to other functions,
‫or used in local scope.
‫Anonymous functions are similar to lambda expressions, but they allow you to specify the return type
‫explicitly, whereas lambda expressions often infer the return type from the context.
‫Here's the basic syntax of an anonymous function in Kotlin, and for example val add.
‫This is the variable name the int int, which are the data types of the parameters, and int is the
‫return type we have.
‫The function fun takes into the parameter a and b.
‫The return type is integer and the method body.
‫Here return a plus b.
‫You can see the return statement is specified in this anonymous function.
‫Also the fun keyword.
‫So the main difference between anonymous functions and lambda expression is you specify the return type
‫explicitly and the fun keyword Lambda expressions often infer the return type from the context.
‫We have a shorter syntax for the anonymous functions.
‫We can get rid of this part so we can specify the variable name.
‫We can specify the function, the data type one and data type two, and the data type of the parameters
‫and the return type with the method body.
‫This is the shorter syntax of anonymous functions.
‫Now let's move to Android Studio and learn about how to create our first anonymous function again,
‫anonymous function is a function without a name, and the general syntax of an anonymous function is
‫val.
‫The function name equals to fun keyword and the parameters with the return type.
‫And here we specify inside the curly braces the function body and the return statement if needed.
‫Let me give you a very fast example.
‫I'll create a list named as numbers equals to list of one, 234, five.
‫Here I'll create an anonymous function that squares a number.
‫So the square of one is one.
‫The square of two is four, square root of three is nine, 16 and 25 for five.
‫Okay I'll start with the basic syntax val or var anonymous function name.
‫I name it as square anonymous function equals to fun keyword.
‫And here I need to pass the parameters.
‫I need to pass only one number at a time.
‫So x it's of type integer.
‫And what is the return type.
‫It's type int.
‫I need to return a square of this number which is an int.
‫Then I'll open the curly braces.
‫Exactly I'm following this syntax the function body function body I can store the variables inside in
‫z equal to x times x, or I can create directly a return statement.
‫So val A-Z equals to x times x and return the value of z.
‫Or I can directly write like this.
‫Return x times x, which is the square of the parameter.
‫Okay, this is our first anonymous function that squares a number.
‫Then I need to call this anonymous function.
‫I'll use a map function to square each element.
‫So val squared numbers equals to numbers dot map.
‫And I'm mapping the square anonymous function.
‫We use this anonymous function with the map function to apply it to each element in the numbers list,
‫effectively squaring each number to the list map is a higher order function.
‫In Kotlin, it takes a lambda expression or an anonymous function, and applies that function to each
‫element of the list.
‫In this case, we are passing the square function as argument to map function and square anonymous function
‫is an anonymous function that we defined here.
‫When we call numbers dot map and we pass the square anonymous function, it iterates over each element
‫in the numbers list, passing each element to the square anonymous function and the map function collects
‫the results into.
‫And you list so squared numbers is a new list which is assigned to the square number variable.
‫So after this line of code is executed, the squared number will contain the result of squaring each
‫element of numbers list resulting in a new list that it, in this case one, four, nine, 16, and
‫25.
‫Let me show you print LN and I need to print this new list called squared Numbers.
‫Run and don't worry, I'll talk about higher order functions in the next videos.
‫And here we go.
‫This is our result, our new list.
‫This is how we define anonymous functions in Kotlin.