 Hey everybody, this is Brian. Welcome to the fifth Flutter tutorial. All right, so we're going to jump back into Dart here where we left off. And if you remember, we left off on flow control. And a lot of tutorials start things backwards, in my opinion, where they will talk about a variable, then instantly jump into functions and classes, which make no sense at the time because you don't know what you're doing. So what we've done is we've started with what is a variable, what is a const final, and then we've gone into flow control. How do you actually control the flow or the execution of your program? And we did the F, L's, for loop, while break, et cetera, et cetera. What we're going to cover now is going to be functions. So what are functions? But believe it or not, you've actually worked with them already. This main right here is a function. Main is actually the quote unquote main function or the entry point of your program. What I have here is just a new Dart program. And it's straight out of the template here as soon as you open it up. And you see how it's got this in calculate here? That's also a function. So really all a function does is break up or chop up the scope of a code. So we can do things like, let's get rid of this because we haven't talked about that stuff yet. Let's say print hello. Just something simple like that. We just want to print the word hello. Well, print is also a function. Somebody has already written that code for you and that's how you're able to call it. So let's say we want to print nine times. So we're going to say four int i equals zero i less than nine. Actually i less than eight. And i plus plus. Let's do this. One, two, three, four, five, six, seven, eight. That count that wrong? One, two, three, four, five, six, seven, eight. I did. Silly me. I had it right the first time. My brain's fried. Sorry about that. It looked funny. That's why I was like, what is going on here? All right. So we just printed nine times. We've done this before and this is old hat. But what we're going to do now is we're going to take this guy and put it into its own function. Actually, let's not call it Tesla. Say hello. So what we have here is we've created our own function called say hello. And it has this weird thing called void in the front. A function is always precluded or preceded by a return type. So we can return a variable from this function if you want. Void means that we're returning nothing. It's just void of any return value. So if you see the word void and you're wondering what the heck to call that, you just simply say the name does the exact same thing. What we've done now is we've broken up the scope of this a little bit better. If we flip over here, one of the things is called lexical scope. Dart is lexically scope language, which means that the scope of the variables is determined statically simply by the layout of the code. And we've talked about this before, right? So here's your top level, which is visible by everything. That's visible by everything. But the inside function is not visible by that. That's called lexical scope. We've already learned that. So what we're doing now with functions is actually breaking this up even further by saying, for example, this I is not available here. We could even say int age equals 99. And we cannot here say print age. It doesn't know where age is because of that scope. Age belongs to this lexical scope or this block of code. So if you're still new and you're still trying to wrap your head around scope, that's really what scope means. So what a function is is basically taking scope, chopping it up even further and saying, I want this function to exist. And this function will only know about things that are inside of this function or this lexical scope right here. So, for example, if I say int age, well, let's not say that in cats equal and there's nine cats. I don't know why I'm fixated on the number nine today. And here we try to say print cats. Well, guess what? It's not going to work. Undefined name cats because it's not in the same scope. So let's get rid of those and get rid of that. So that is a very, very bare-bone basic example of, you guessed it, a function. Now, what we're going to do here is we'll say, avoid annoy. We all have that one annoying person, right? In our lives that just loves, loves, loves to talk. And we'll say int times. And here we're going to just say for int i equal zero i less than times i plus plus. I think my music stopped playing. Did my music stop playing? Nope. It's just very quiet for some reason. I have to play music in the background or this wireless headset will actually shut down on me. It's kind of weird. So some videos you'll actually see me flip back and forth. But so we've got our four statement here. Very similar except for instead of, you know, just hard coding it, we're doing this. So we're going to say blah. So somebody says hello. Let's actually leave that the way it is. But then the annoying person, annoy. And let's say they come up and they just start talking. Blah, blah, blah, blah, blah. You know, we've got a thousand blahs in there. I mean, at this point, you just want to like walk away and tell them to go bye-bye or something. So what we've done here is we've actually passed a variable to this. Believe it or not, this is a variable. Someone says it's a value. But remember everything in Dart is an object. Therefore that is actually a variable under the hood. This is called a parameter right here. The cool thing about parameters is what we're doing here is we're actually creating another variable and that variable exists within the scope of this function. You can see how we're accessing it right here. And we can actually say things like, let's get a little funky here. Eye of my phone made a weird noise that I never heard before. So here we're going to say, you know, what number of the number of times we're supposed to do it and then they're still talking. So let's just run this again and you can see. So we obviously have access to this variable. And there's really no limit to how many times you can actually do this. So what we can do here is we can say, avoid really annoy in times. And then we could just say, let's grab this. Now this may look a little weird and I wanted to do this as an example. So let's grab this and let's just comment these out here. So what's going to happen when we execute this code? Well, really annoy is going to get called, which is this guy here. What really annoy is going to do is it's going to actually call annoy, which is going to do this. So it's five by five. You can see how it does this and it just goes over and over and over again. So that is a function calling, a function calling, a function kind of thing. You have to be kind of careful when you're writing functions because you have, and some people are going to argue this isn't really the case, but you have a bit of encapsulation. And what this does is this gets a little confusing if you're not used to scope. For example, in really annoy, we have int times and times is here and times is here. However, this times right here, while it's the same value is probably not the same variable unless starts doing something under the hood where it, you know, because it's the same value that moves it in memory and blah, blah, blah. It's not the exact same thing. It's got a completely different scope and therefore has a different access. So we can actually change this name to blahs. And notice how now we've got to change it here and here, but it does the same thing. So looking at the functions inside of the tour of Dart, you can see there's other things like bool. And of course, now that I'm looking for it and there's void string, things like that. So you can actually make some really cool functions. So what we're doing here is just recatenating two strings together, actually technically three, because this is also a string and returning a brand new string object. So let's go ahead and do this. Okay, so we have my first name and my last name. And you can see how it just prints out the string Brian Caron's because what we're doing here is we're calling this function, which is taking two parameters now and returning them as one variable. Now, if you're wondering what types of variables you can return, well, any type of variable, any object, actually, you could actually even say there for unknown type. Ooh, actually, no, you can't. The return type can't be there. I stand corrected, ladies and gentlemen. I thought you could do that, but you cannot, which makes me wonder if you can return an object. You can return an object. Okay, so if you have no idea what you're returning, it's just some thing you can return an object. But in general rule of thumb is you don't want to do that because then they have to either know the type or they have to use something called reflection to go in and figure out what type of variable it actually is. Dart is a static type language, meaning that you have to declare your variable types. So let's go back to the tour here. There's some really good examples. And if you're not familiar with any of this at all, you should really kind of just play with some of these and figure out what they do. One thing you can do is default parameter values. And this is really kind of built into almost just about every language here. And what you can say is, you know, your function can use an equal sign to define the default values for both the name and the positional parameters. The default values must be compile time constants. Must be compile time constants. If no default value is provided, the default value is null. Meaning, because these do not have a default value, they are null. Null means there's just nothing there. It's like nothing else. It's like when you lose a puppy, you're just empty and void inside. So what we can do here is where to go is we can turn one of these into a default value, right? So let's just say this. Not good at call of duty. And it says positional parameters must be enclosed in square brackets here. So what did we screw up here? That's a good question. What did we screw up here? This thinks it's optional here. Let's try that. Brian Carons. Now, if we admit this guy here, we suddenly get Brian not good at call of duty. So what we're doing here is we are saying, this will be null. We are not assigning a value to it. It must be assigned and there it is here. So if we just delete this, you notice how suddenly it's going to freak out and say one argument is required but zero found because this is the required argument. We haven't supplied anything and you can't actually do anything with it. So you could actually pass null which may actually make this thing explode. Let's find out. See the method blah blah blah was called on null. So we're saying null plus these strings. So Brian, let's just change this to is not because I'm really not good at call of duty. I've been trying to get better at it but I'm not good at it. Let's run this again. So we've supplied the first value and it's not null. And then we have this optional and we are assigning the value to it. So because it's optional and you have to include any optionals in these brackets, you don't need it. You see how there's no name here but the minute we include it and let's say let's just do my name again just because why not run this then suddenly it is not using the optional parameter because we have supplied the value. Once this value is in here gets converted to a variable and it replaces this. So think of this right here as like a temporary variable and let's say let's just call this string extra. Let's make that lower case just because I'm kind of snobby about that these days. And we're going to add the extra in there. So you can see how we actually can do this, right? So we have Brian which we've supplied Karen's which we've supplied. We did not supply the extra. Therefore it's going to use the default and we could actually take this out right here and you'll get two of them. See how that works. So as you can see, functions are pretty neat. There's a lot of things you can do with them. One thing you should really be aware of when you're creating your functions though is the actual scope of the variable because sometimes things can get a little squirrely and you're looking at one function and because you've named the variable the same in many functions you think that variable goes everywhere and it really doesn't. So that's all for this tutorial. I'm going to keep going down. There's actually a lot to cover on functions. So we're going to make a couple of these. But if you found this helpful or even moderately entertaining go ahead and visit voidrums.com for the source code for this and all other tutorials. And if you're so inclined go out and join the voidrums Facebook group. There are 1720 members out there right now all walks of life all programming languages. So definitely check that out. Join up become a member and help everybody else out. Thanks for watching.