 Hey everybody, this is Brian and welcome to the sixth tutorial on Flutter. We're going to continue down our path with Dart. I'm going to try and go through these fairly quickly, but because Dart is a new language and there's new features, we've got to go through some of this elementary stuff. So let me more advanced folks just bear with me while I slog through this. There are some new features, so definitely pay attention and I'll try to call them out when I get there. So this is going to be our advanced functions tutorial. So the first thing we're going to do is just create a new project here. Make sure it's Dart console application. And let's call this advanced funk. Why not? I have a really hard time coming up in names for this sort of stuff. And it'll process and do its thing. Tick-tock, tick-tock, IntelliJ is always so slow. There we go. All right, so we've covered some of this stuff before. We've got our calculate function, which is off in the lib. And we're going to actually use that this tutorial. So if you're curious about how that works, definitely pay attention. All right, so we're going to go ahead and just get rid of this. We're going to leave this up here this time. And we're going to cover that in just a second. But let's get through the rest of this first. So in our main function, we want to make a variable. And let's just say list, my keyboard's crooked. Bear with me here. This is what happens when you get old and cranky. So we're going to make a list with an integer type. Call it list. And if you remember, these little brackets with the type in it is generic programming because that list can hold more than one type. And then in our list, we're just going to add a few values. So we'll say one. And I'm not going to really add too many variables. I'm actually just going to use this one variable because this is a function tutorial and I don't really want to spend a lot of time wasting your time writing variables. So I know a lot of tutorials out there have these big, weird, complex algorithms that are hard to understand. We're not doing that. All right, so we've got our variable that we're going to work with. So the first thing we're going to cover is a function as an object. Maybe if I spell object. So what does that really mean? Let's just make our function here. Nothing's super special about this, right? And we're just going to say print. All right, there's really nothing going on in there. If you remember, everything in Dart is an object. And I mean everything, right down to functions. So we're going to say list for each. Now you notice how there's a little bit of verbiage here for each. And then there's parentheses int, and then arrow, and then void, something, something. We're just going to double click it from the moment. And you see how it says void f element, e element, sorry. So really what that's saying is it cannot return anything, void. And the function itself is going to take the element. Now the element goes back to generic programming. Under the hood, this actually says something like this. List e, and e would be the representation of whatever object we're shoving in there. So the template for this thing has to be void, cannot return something, and then it has to take the element. So we're just going to say as object. Finish this off with our semicolon here. And let's actually, because we're going to make a bunch of these, just finish watching Stranger Things last night. Oh my god, that show is so good. But now I'm trying to find other things to watch, like a Netflix junkie, all right. So when we run this, this is what happens. It says as an object, bam, and then list for each. And then we're calling the actual function as an object itself. This object is instigated when the code is run. So we don't have to do this new, blah, blah, blah. It already exists in memory, and there's one copy of it. So as object, and then that has to follow the template. So if we do something like this, it may get kind of Frankie on us and say, hey, nope. Yep, see, argument instring void cannot be assigned, blah, blah, blah, blah. Basically, what that's saying is it has to follow the template. And the template is you're passing one element, which is in that list. So that is a function as an object. I tend to not use that too much, because then the code gets kind of weird. You're trying to hunt down this thing and figure out what's going on. Go through it, then I guess it's OK. What I tend to do is I'm just going to copy and paste this whole thing rather than type that all out. Bang. As an anonymous function. I'm lazy. I really didn't want to spell anonymous six times. This keyboard is kind of jacking with me. I need a new one. I was playing Call of Duty, and admittedly, I slammed my fist down on the keyboard, and now it's acting kind of crazy. Anyways, so we're going to do four each. Now this time we're going to do something a little different. We're going to do what's called an anonymous function. Now an anonymous function has kind of a crazy syntax, but it'll save you a lot of headache. What you can do is say four each, and then we're going to just say parentheses i, and then bracket. And don't forget to close this out with a semicolon. And we're just going to say print i. This is bananas. What is this? Let's run this, make sure this actually works. So as an anonymous function, bang, what the heck is going on here? So what we're really doing is we're creating a function in memory that only exists in this little loop here. It actually under the hood gets optimized, and it turns into a temporary function in the base code or whatever. But for our purposes, we don't have to jump around all over the place. We just know that, hey, we want to do this, and we want to do it right here. So it does the exact same thing as this. It's just you don't have an independent function that you can call as an object. Notice the syntax is we have our parameters, and then we have our brackets. We have our scope block here, and then we have our actual code of what we're going to run. It's pretty simple, pretty elegant. I actually hated anonymous functions when I first saw them. But I think I first saw them in Java, and they just drove me nuts. But they're pretty useful. As you can see, you don't have to make yet a separate function now. If you're going to call this many, many times, like over the course of your entire program then definitely branch it off into a first class function right here. So now we're going to see what I mean about my keyboard. Eternal, external, why can you not spell keyboard? I'm going to Best Buy and get the new one. Forget this. External function. So what is an external function? We've been talking about this little file over here for a while now, let's crack this back open. You see how it's got a function. It returns an integer as called calculate, and it has no parameters, and then it just returns six times seven. We can actually call that externally. So let's just say print, and how do we know what to do? I mean, how do we call that thing? Can we just say calculate? Notice how when you start typing calculate, it recommends advfunk.calc. Where did it get that name from? Let's just double click it so it puts it in there for us. It actually got up here from this import statement that we left, and if you accidentally delete it, go ahead and retype all that in. But it's very simple, import, and then we're importing a package, semicolon, and then the path to that package. Advfunk is actually the name of our project here, advfunk, and then the actual Dart file, advfunk.dart down here as, and this is where we're creating the variable. So really, it's that. And we can run this, and you can see the result of that is 42. So let's crack this open. Let's just add another one here. Let's say bull loves this keyboard string name. So if the person in this function is Brian, me, then I do not love my keyboard. Otherwise, we're going to return true because everybody else in the world loves their keyboard, especially when they're playing Call of Duty, little bastards. All right, so as you can see, loves this keyboard. And then we could say, I'm so buying a new keyboard and there's our false. So how would you actually go about making your own here? You can actually make a new one. We'll just call this Dart file, let's call this myfile.dart, and rather than typing that out again, we're just going to cut this, put it in our little file. Notice how when we did that, suddenly main.dart got cranky because this no longer exists. So now we've got to make another import. We're importing a package and there's different types of imports, but we're just going to cover the package for today. And notice how it actually tries to help you out. So we're going to import advfunk slash myfile.dart, the file we just created. Now we need to make it as a variable and let's call this call of duty. It is currently grayed out because it's unused. We're going to actually change that, bang, now suddenly it's used. Does the same thing. So that is how you would import an external function. Now, the next one is going to get a little bit crazy, but this is the last one in our little tutorial here. We're going to compare apples to oranges. You've heard that expression before if you're in the United States or maybe even if you're out of the United States. So we're going to make optional parameters here. And we're going to say int apples equals zero and int orange is equals zero. And then we're just going to simply say, compare to other number. And actually we don't have to do any of that. We can just return it. I had different in my little notes here of how I did it, but this is actually, there we go. Maybe, there we go. That didn't work either. What did I do wrong here? Richard type int, oh, that's why, because it's doing an int type. So the compare to function is something I ideally didn't want to use, but basically, let's just switch this just so we can see. Compare to is used for ordering in list, which we'll cover in a future tutorial, but let's just do this. What we're really focusing on is this part right here. We have our function and all this does is it has optional parameters. Notice these are optional. They're optional because they're within these brackets and they have a default value. And we're just going to say, are you possessed keyboard? What are you doing here? Geez. All right, so naming a parameter. And we're going to just say, compare. Notice how you can actually put the parameter name in there and then hit enter. And it's giving us a negative one because they're not. So let's just change this to nine, that to three. You see how it's now a one because it does an ordering thing. I was going to use a bull, but let's just say this is six and this is six and it should come back as zero, meaning they are equal. So why would you want to actually name your parameter? Why can't you just do the order here? Very simple. A lot of more advanced code will actually use name parameters. When you get into Flutter, you'll actually use them quite a bit. And it's just an elegant way of saying, hey, this is actually going to be this. And I just want to test this. I want to actually move this around. So you notice how we've actually got it backwards? In the compare statement, it's apples then oranges, but we're actually seeing oranges than apples. This would normally throw an error in other languages, but should run just fine and it does. So that is also why you would use it. If you don't want to use an ordered list or an ordered list of parameters. So more information, go out and read the official documentation in optional parameters. And you can see how when calling a function, you can specify name parameters, et cetera, et cetera. Definitely read up their documentation. It's well worth the read. I know it's a little bit lengthy, but I can't possibly cover everything in these videos. If you found this educational entertaining, the source code will be out on the website. You can just click for the link out to GitHub. And there is a Voidrums Facebook group with I think 1700 other programmers out there. All walks of life. We can definitely help you out with any issues you come up with. Unfortunately, I get just an extreme amount of email. I've actually had to make a separate account and hide it from the world just as I couldn't do anything with it. So don't email me. Go out to the Voidrums Facebook group and go out to GitHub and get the code. I'll talk to you guys later. Thanks.