Cross-Platform Mobile Apps with Flutter

Functions & Lambdas

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Cross-Platform Mobile Apps with Flutter

Check out a free preview of the full Cross-Platform Mobile Apps with Flutter course

The "Functions & Lambdas" Lesson is part of the full, Cross-Platform Mobile Apps with Flutter course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through function syntax. Arguments can be either positional or named. If a function uses named arguments, they can be passed in any order. Lambda functions use a fat arrow and can only return a single expression.

Preview
Close

Transcript from the "Functions & Lambdas" Lesson

[00:00:00]
>> Functions are pretty straightforward. And mostly if you're coming from JavaScript, it's just the same, but without the function keyword, okay? So we just write the function. Something that we have is an implicit return type that is empty here. For example, you can explicitly specify that you need to return something from this function.

[00:00:22]
You can use void to should say that I'm not returning anything. And that's the default operation when you don't specify the type, or you can specify the type that you want to return. For example, I'm going to return a string. This is similar to Java or to C, C++ as well.

[00:00:41]
You put the type that you'll return before the name of the function, okay? Does it makes sense?. And of course, if you have a return type, you must return something from the type, if not, it's not gonna work. It's not going to compile. So far, so good, I guess.

[00:00:58]
What about arguments? Well, arguments work like this. We use the type as well. We cannot use var here, because it needs to know the type of the argument that you're expecting, okay? So that's the simple way to create the sum function. It receives two integers and return an operation over that integer.

[00:01:21]
Remember, this is the return type. So what you are returning actually must be of that type. This is new, something that have that is the ability to define named arguments. So what is the idea? The idea is that I can create another version of the same variable, for example, asking something like this.

[00:01:48]
I can say that I want this argument should be named as a and b. They are, by default, optional, but if I want them to make mandatory, I can apply a require property first. I know that this seems weird, so let's create one here, so we can understand what's going on.

[00:02:13]
So, for example, I can have here a function that will print a customer. And let's say that the customer will receive the name. So I have a string name, and it has an age or, I don't know, a balance, let's make it double, a balance, and if it's active, it's going to be a bool.

[00:02:35]
So I'm going to print this, just that, okay? So print, I can print comma separator, so the name, the balance, and isActive. How we're going to call this function, Well, it's complaining here because I need to convert this into a string, because print receives only strings. Something quick, a quick way to convert anything to a string is to use a string and then the dollar sign inside, for example, okay?

[00:03:03]
That's a simple way to do that. I might have other issues here, but anyway. So also, you can make separate prints, because it will be simple to understand now that I'm going to change the signature of this file. So how do you call your own function? So if I wanna call print, it's printCustomer, and then you pass the arguments.

[00:03:28]
So, simple, right? So I have Jane, with balance, and it's not active, so it's false, semi-colon. So that's one way. Okay, cool, but what we can do is make some arguments optional. How to make the arguments optional, I can put them within curly braces. If you do that, okay, now that becomes a name argument, and name arguments are optional by default, okay?

[00:04:03]
If I want it to be mandatory, I can use required. You will get what this is in a minute. So I added now, I have two standard arguments and one named argument. What's the difference now is that the third one needs a name. So I actually need to say isActive: false So when you create a function, you need to make a decision.

[00:04:36]
Do I want all the arguments as usual, like classic arguments? Do I want all of them with names? Or I can make a mix. The first argument is unnamed, and then I can make the double also here. I put them in the same curly braces, and I can make them optional or required.

[00:04:57]
So now, 200 doesn't work, and I can say balance:200. The idea is that now we can make the function call more semantic, because now I say, I'm going to print Jane, but it has a balance of 200, and it's currently not active. Does it make sense? And you can make all of them like this, named.

[00:05:24]
And when they are named, I can also change the order in what I'm calling them. Because they have a name, they know where that goes. So, of course, balance will go to the second argument. Does it makes sense? I mean, it's not complicated. It's different, and Flutter is heavily using this, okay?

[00:05:47]
So we are going to see this a lot within Flutter, named arguments. Make sense?
>> What happens if you took Jane out?
>> It's not gonna work because it will tell you, I mean, the signature will tell you that there is one positional argument expected. And then you have positional arguments and named arguments.

[00:06:08]
Positional arguments are the ones that are based in their position, okay? Now, we are not following the signature, so it's not gonna comply. It's mandatory to set the name as the first positional argument.
>> So that string name is almost required.
>> Yeah, all of them are required.

[00:06:29]
>> Yeah.
>> Okay, because I said so with the required keywords. So all of them are required, but the last two arguments, you can put them in any order, and also you must name them. That's the difference, okay? To finish the function sanction, we also have lambda expressions or anonymous functions.

[00:06:52]
In that case, it's a variable of type function, capital F. Of course, I can use var as well, instead of this. And how does it work? We're going to see this a lot within Flutter. So we will remember this a couple of times. We use parentheses with the arguments declaration inside, and then the code in CodeBlock.

[00:07:18]
So this is a function. And it receives number and it's ready. But this is a variable containing a function. So I can pass this as an argument, I can receive the second return, one of these. This is like writing in JavaScript something like this, okay? When you create an anonymous function like that in JavaScript, this is the equivalent, okay?

[00:07:44]
And because we have arrow functions in JavaScript, we also have arrow functions here in Dart. The only difference is that we use arrow functions with the same fat arrow equals greater than only for one expression. So we use CodeBlock when you have several sentences, several expressions. And we use arrow functions like lambda, quick lambda expressions, only accepting one sentence that has an implicit return.

[00:08:17]
So there is an implicit return here, okay? So let me show you this. If I wanna use the same function without this, I put this in a CodeBlock and I add return. And semicolon. So, actually, semicolon here as well. myArrow and myArrow2 are doing exactly the same, but with different syntax.

[00:08:42]
Does it make sense? So the arrow function is smaller and faster.
>> I'm just thinking, I mean, this is only the case where you actually have to use the keyword function in this type.
>> Well, actually I don't need to use the keyword function, because I can create a variable or a final with this.

[00:09:03]
That's not the keyword, that's a type.
>> Okay.
>> Okay, that's the type.
>> Okay.
>> That's the type. Bu it was a good one, right, because, yeah, it seems like a keyword, but it's not actually a keyword, it's the type. The variable or the mutable variable or the constant is actually of type function.

[00:09:19]
But I can use implicit typing, and now I'll need to set the type. And you can see here that the type of my arrow, it's a function with receiving joins. Yeah.
>> So the functions that you created earlier, they don't have semicolons after the function definition, but these do, because they're a block?.

[00:09:40]
>> Yeah, so the question is, actually, why do I have a semicolon there, right? So because, think about this, I have a variable integer, okay? So I have my age, it's 4? Do I need a semicolon at the end? Yes. Now, instead of being a 4, it's a function.

[00:10:00]
So I open this O arrow function, so how do you do this arrow function? You do this. So I still need the semicolon because the semicolon goes to the var declaration sentence. If you do that in a separate lines, you still need the semicolon anyway. Okay, that's why you have the semicolon.

[00:10:22]
And when you have the normal function declaration, you don't need a semicolon, because you don't use this as an expression, it's not actually an expression, the sentence within the language. It's a declaration, so it's something different. Anyway, the good new is that the IDE will tell you that if you're missing the semicolon, okay?

[00:10:42]
And there's a quick fix, that on Visual Studio Code is Ctrl + dot, or Cmd + dot on the Mac. And on Android Studio, it's Alt+Enter or Option Return. So if I say here Cmd + dot, it will say, okay, I wanna insert the semicolon, yeah, okay? So the IDE will help you.

[00:11:03]
We are going to use the IDE lot to improve our productivity in Flutter, okay? Make sense? Yeah.
>> Was there a reason why they do a single expression in their fat arrow?
>> Where, which line?
>> So line 67, I'm kinda comparing that to arrow functions in JavaScript, because you can have multiple commands.

[00:11:28]
>> Yeah, here, if you use the arrow function,
>> Yeah,
>> It's only one expression that has an implicit return.
>> Yeah, do you know the history on that or why they do that?
>> I don't have the history, but the thing is that if you don't want that, you have this.

[00:11:42]
I think this is coming from other languages, not from JavaScript, okay? So I mean, if this was JavaScript, it was like this. While here, when you have multiple and we have the code block, they say, we don't need the arrow function, we just remove the arrow sentence. So if you ask me why they're not using the arrow for the code block, I don't have the answer for you.

[00:12:05]
But there are many languages that is like this, that the arrow is only for lambda expressions. And the definition of a lambda expression, on some languages, is actually just one line, just for functional programming. And for that, you don't wanna do multiple sentences, only one. And I think we have to live with that, picking different syntax for different situations.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now