Check out a free preview of the full Introduction to Kotlin and Android Development course:
The "Functions & Lambdas" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through creating functions with the fun keyword, inputting arguments, extension functions, and lambda functions. Lambda expressions and anonymous functions are function literals that are not declared but passed immediately as an expression.

Get Unlimited Access Now

Transcript from the "Functions & Lambdas" Lesson

>> Okay, functions and Lambdas. So a function it's similar as the functions that you're used to with a little, some tweaks on the syntax. For example, instead of using the function keyword, it's the fun keyword. Some people say fun, it's up to you, but let's say fun, fun is funnier, I guess.

[00:00:21] So fun, the name of the function and parentheses as you're used to, Aniko look. If the function returns a value, we must say so by using column and the datatype that we are returning, okay? By default, we're not returning. So if you either return is not gonna work, it's not gonna compile because the signature of the function doesn't say that is returning something, okay?

[00:00:50] So we must add column sometimes you add a space here. In that case you can pick the one you prefer. Something interesting that we have is that if the function has only one line, I can get rid of the return first, okay? But also I can get rid of the code block.

[00:01:10] And instead of a code block, I can use equals. So in that case, it's a one-line function declaration. And there is an implicit return, the equals it's kind of an implicit return. So calling a function returning to is equals to that string, that's the idea, okay? It's not mandatory, is something that you can picture use.

[00:01:37] Arguments, arguments are actually pretty straightforward with the differences in syntax that we have in Kotlin. So we use the name of the argument colon and the type coma. The name of the argument colon and the type. We can have default values if you want to, okay? So that's fine.

[00:01:57] Even if you have a default value, you must declare the type, okay? So you cannot use implicit type here. We must declare the type in this case. So in this case, actually pretty simple. How do you call some, as you're used to. Like so, but look at what happens here because the first time you see that it's confusing.

[00:02:22] So there isn't a colon, like that, and a b colon, okay? But I didn't type those. Actually, those are not characters in the file. Be careful with this, okay? So be careful, mostly when you are looking at my code, when you're typing, you don't need to type a colon and b colon because they're not there, okay?

[00:02:45] It's just a hint from the IDE, okay? So, you can disable that if you don't want, but also this is interesting. You can also call the same function naming the arguments, but instead of column width equals. You decide when you call the function, if you wanna call the function in the classic way, like with values comma separator, or by naming its arguments with the name equal and the value.

[00:03:22] It's doing exactly the same. Is that your question or not?
>> Yeah, does that mean you could switch them?
>> Good question. That means that if you're using the second version, you can actually change the order of the operators. So the order doesn't matter when you're naming them.

[00:03:39] To make things even weirder, when you have another one, let me clone this one with a third argument. Like so. When you're calling let's say some three. When you're calling that one, I can say I want a and then, so that doesn't have a name a, even if you see a colon that's not a name, I'm not naming the argument.

[00:04:04] And then the other two, you can change the order. So the first one is not using the name and the other two is using the names. And by the way that feels weird, but then we are going to use this a lot in jetpack compose. A lot more than you want [LAUGH] some points.

[00:04:23] Okay, this idea of naming the argument, or sometimes we name the argument, sometimes we don't name the argument.
>> Is that a colon? Is that a IDE hint or-
>> Yeah it's an IDE hint, so a colon is actually not there. In fact, if I'm moving this you can see the cursor.

[00:04:39] I mean, I cannot delete that. If I try to select a, I cannot do that because it's not there. If you open that file from VS code, you're not going to see a colon. So again, have in mind that a equals is something that I wrote and a colon is something that I'm not writing and it's not there in the text file.

[00:04:59] You can disable that hint if you don't like it, okay? There are many hints like that like those in Android Studio, okay? Well, that's simple to get, I guess. Then we have extension functions, that is actually pretty cool. Not every language is letting you do this. An extension function is a way that we have to inject to have new functions to any object or any time, even if you don't own the type.

[00:05:31] So in this case, I'm adding an even method to integers. So now you will see that I can 2 is even to 4, because it's an integer. That's something I have just created. The only difference is the function that you write anywhere, but you have a prefix with a type dot.

[00:05:53] This is heavily used in Kotlin. Heavily used in Kotlin. So, sometimes you will be using functions that you don't know where the function is coming from because when you get into the type, the function is not there, and say, what's going on? Well, the function is coming from an extension.

[00:06:15] And I am saying careful OOP extremist because think about it from an OOP perspective, from an object-oriented perspective. This is a thin, actually, right? Because you're messing with other types that you don't have permission, right?
>> It's like a global object.
>> It's like a global object but receiving the integer as an argument, but instead of doing it like so, we're doing this other method that has its own unique advantages.

[00:06:43] Okay, so we're going to see this a lot during the rest of the training, and finally we have lambda functions. That's the name that we use here for literal functions or anonymous functions, closures in other languages. Here it's called a Lambda function. So a Lambda function it's a variable.

[00:07:02] It's a data type. The data type is actually when you create a lambda, the data type is parenthesis, thin arrow and then the return type. Now for example, if I have receive two integers and return an integer, that's how you express that, okay? This's actually pretty similar to swift and maybe swift is the only one that looks exactly the same like this, the data type.

[00:07:28] What happens if you don't wanna return anything, weird. There is a void, but we don't use that in Android. Here we have one difference between compiling for Android and compiling for other platforms in Kotlin. It's called unit, and please don't ask me why it's called a unit. It's a weird name.

[00:07:50] But unit means that we are not going to return a value in that function. Okay, for Android SDK. And how do you assign a value? Code block. So you open curly braces, and you put the code inside. How do you curl the function? Like any other function. You just curl it.

[00:08:18] Okay, something interesting about arguments. Let me put this here, if I have in this function I receive an integer, okay? If you see here I have a new hint. That says it int. If you have only one argument one and only one argument, that argument has an implicit name of it, so you can print it.

[00:08:48] So the idea is that it is the option that we are receiving, the only option that we are receiving, okay? So you can print it, you can save it, you can, wherever the with it. If you don't like it, okay? You can rename that implicit int. But the idea is that this will improve or it will improve the speed of your coding having that int there, okay?

[00:09:12] So instead of doing that I can actually put a name on it. And it's an integer, so I will call it int. And we use the arrow, the thin arrow as a way to separate the name of your argument or arguments and the code, and that goes within the code block.

[00:09:35] So within the code block, not outside of the code block, okay? So if I have two arguments, an integer and a Boolean, well, I can just name the Boolean, like is radio wherever. So now I don't have int, I have is already an int, my two arguments, okay?

[00:09:58] So I think here I have an example like that, but it takes some time to get used to this syntax because I know the first time you see at this, it's like, it takes some time to read this, okay? But actually it's a variable with a name myCalc that I know it's a lambda expression because this is the whole type.

[00:10:21] So that's the type, the type is a lambda expression. I know that because there is an arrow, the lambda expression receives two arguments, two integers and returns an integer. And then we have an equals, an assignment. So we have actually the current value of that variable that is actually this code.

[00:10:40] And that code can be like this, but because there is only one line, I don't need the curly braces, like we saw before. So I'm saying that I'm receiving two arguments a and b, and I'm returning a plus b. So as a lambda expression that you can quickly write a lambda expression for functional programming, okay?

[00:11:06] Any question?
>> So as soon as you have more than one parameter or variable, it is-
>> No, you're forced to name them. So if you have more than one argument you're forced to name the arguments. It works only with one argument.
>> Is that kind of like this?

>> No, because you still have this.
>> You still have this?
>> Yeah, when you have a context for this, like a class or an object, you still have this, so.
>> But it's functional we don't have this?
>> Well, let me change your question. So is this functional programming or is Kotlin functional programming?

[00:11:42] You can kind of use some of the functional programming ideas, but when we are compiling for Android, we are compiling for a Java VM or for a BM compatible with Java, let's say. And on Java, everything is a class. So actually, if you wanna execute this code like so without the class, in Android, it's not gonna work.

[00:12:10] In android, you cannot create global variables. Everything must be within some sort of class or structure because of the restriction of the Android BN. But if you're writing Kotlin, for console or for native, you can actually do whatever you want. But just remember that Kotlin for Android is working within the restrictions of the Android runtime that is compatible with a Java VM, at least in terms of the architecture.