Hi. In this section, we're going to learn about functions in Scala. What is a function? Functions are very much like methods, they have parameters and they return results. In fact, we've already seen an example of a function. In the previous section when we were looking at lists, we saw the map method, and map takes a function as a parameter. We have an example here putting map on a list, and here is a function literal, which is an expression that evaluates to a function. In this case, it's a function of a single parameter which is contact here. The result of calling this function is the result of this expression, contact.name. Let's see some more examples of functions from which we'll learn more about how we declare and use them. In our first example, we're declaring a val, which is like other vals we've seen so far. We have the keyword, val, we have the name we've given to the value, and then in this example, we have the type. In this case, we see our first example of a function type. We have on the left-hand side of the arrow, the type of the parameter, in this case, a single parameter of type, Int, and on the right-hand side of the arrow, we have the type of the result, in this case, also, Int. Then we have the equal sign, and we have the expression which constructs the value. In this case, it's a function literal, like we've seen before. We have a single parameter x on the left-hand side of the arrow and on the right-hand side of the arrow, we have the expression that computes the result. In this example, we're just adding one to the input parameter and returning it. In our next example, we're doing more or less the same thing, but we don't have a type here anymore. No type here. Scala will infer the type like it does for other types of values. However, Scala cannot infer the types of parameters, so because we haven't given the type here, we have to declare the type of the parameters here, x and y, and we're seeing the types are, Int. Notice when we have more than one parameter, we must enclose the parameters in parentheses, like we have here. Then on the right-hand side of the arrow, it is the same as before, the expression that computes the result. In this case, x plus y has type Int. Scala will infer the result type, but not the parameter type. Now we have the example of using the three functions we just declared. Now, this function application is the same as method application. We put parameters within parentheses and we work from the inside out. Here we are calculating increment(2), which is going to be 2 + 1 = 3, and then add 1 and 3 gives us the result 4. Now that we've seen some examples of functions, let's extend that to the general case. Let's say we wanted to find a function with parameters t1 to tn. What we do is write the parameters separated by commas, then separated by parentheses. That defines our parameter list. We then have the arrow and we have the expression which defines the body of the function. Now this expression could be a single expression. If we want to have multiple expressions known as a block expression, we either need to use indentation to indicate the block or we can surround it by curly braces. Using indentation is the non-idiomatic choice. This is the function that takes n parameters and returns the expression e. Sometimes the compiler won't be able to infer the types, so we'll have to write them out explicitly. The syntax here is the same as in the rest of Scala. We have the parameter, the colon, and then the type, and repeat that for all of the parameters. Now, a function being a value also has a type. It's called the function type. The syntax for a function type looks the same as the syntax for a function literal except we're replacing types with parameters. Here we have the type t1, t2 to tn, and the types of the parameters, lowercase t1 to lowercase tn, the arrow, and then the return type. Now that we've learned about functions, let's have a little quiz to test our knowledge. What is the type of the function shown on the screen? Is it contact to string, contact to boolean, string to contact, or string to boolean? Pause the video while you decide on your answer. The answer as given on the slide is contact to boolean, the second option here. How could we work this out? Well, the parameters of the function are explicitly given a type. We have here the parameter contact, colon indicating a type, and then contact, which is the type. We know that it can't be these two options here because they both have a parameter of type string, so we're left with the two options of parameter of type contact. Then we have to distinguish them as the result of type string or the result of type boolean. Now, this function consists of only a single expression, so the question comes, what is the type of this expression? In particular, this method endsWith, which is the final method called in the expression? The answer here is endsWith returns a result of type boolean, so it must be this answer, contact to boolean. At this point, we've learned quite a bit about functions and you might well be wondering, why do we have functions in the language. It seems that they do the same thing as methods, and that's a really good question. Let's see an example. Here we have a method which is called increment, has a single parameter as integer and returns one plus that parameter. Then right below, we've to find a val called increment, which is bound to a function, has a single parameter and returns one plus that parameter. It seems that the method and function form, do the same thing. In both cases we have the same syntax to call them like as follows, so what's going on here? Shortly, functions must give us something additional beyond method if they're in the language. The answer is yes, functions do give us something beyond methods. But the difference is quite technical and it comes down to Scala being a language that supports both functional programming and object-oriented programming. The key difference between a function and a method is that a function is a value, what this means is that you can pass a function as a parameter to a method or a function, and you can return a function as a result from a method call or a function call. You cannot do these things with methods. You cannot pass a method to a method, for example. Now in Scala, all values are objects, so this means that functions are objects. That means I can also have methods on them. When a function is created, we must allocate memory for it. I said that functions are objects, which means we can call methods on them, so what are some of the methods that are available on functions? By far, the most important method on functions is a method called apply. When we call a function as shown here, what actually happens is we call this apply method. In fact, the compiler rewrites what looks like a method call, a function call into a call to the apply method. This is really important because it means there is a consistent model underlying how Scala works. There's no magic in functions, they are just objects like other objects, and we interact with them by calling methods but the one thing that Scala gives us is this shorthand syntax. Whenever an object has a method called apply, we can just drop that call and write it as if is a function call and Scala rewrites it into a call to the apply method. It doesn't matter if it's a function or not. Anything that has an apply method allows us to write this shorthand. Now we understand the difference between a function and a method. There's a bit of good news that a lot of the time we don't need to worry about the distinction because the compiler will convert methods into functions when it can see that they are needed. For example, the map method has a single parameter, which is a function. If we call map using a method as shown here, the method increments. The compiler will convert this method into a function for us, and it can do that because you can tell that the type of the parameter is a function types so function is needed. That's it for functions, now let's have a recap of the most important points. Firstly, functions are values. What this means is that you can pass a function as a parameter to a method or return it from a method, and you can't do either of these things with methods. Methods are not values. We can define function literals using the arrow syntax, and what this means is the left-hand side of the arrow, we have the function parameters and have an arrow, and on the right-hand side we have the expression that calculates the function result. We have similar syntax for the type of functions. On the left-hand side of the arrow, we have the type of the parameters, the arrow and the right-hand side, the type of the result. Function being a value means that it's an object in Scala, and being an object means it has methods. The most important method on a function is the method called apply, and calling a function is just a shorthand for calling the apply method. This is how Scala unifies these two seem to be different concepts.