Golang Tutorial #17 - Advanced Function Concepts & Function Closures

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome back so in this video I'm going to be talking about functions going into some more advanced examples and then covering function closures later on in the video so the first important thing to understand about functions that we haven't covered yet is that functions are actually their own data type now what that means is they act very similary similarly to things like int floats strings boolean's and that means that I can actually assign them to variables I can pass them in as parameters I can return functions and I can just send them around my program which actually happens to be very useful and go left so let me show you what I mean by this when I call a function I do something like this right I go X colon equals test this is just a simple function that I coded out here what this is saying is okay let's call the test function oops what the heck test ah sorry I've got to do this shouldn't be assigned to variable but when I do these two brackets I'm saying let's call the function let's tell it to execute let's maybe pass it some parameters or arguments if it has anything here let's maybe assign it to a variable and get some value from it but this is calling the function which means whatever's inside of it's gonna run a function will run now that is different than just writing the function without the parentheses so these parentheses signify a call now if you don't have those there what you're saying is not call the function but reference the function which means almost like make a pointer to it like say okay this is the function you know this is where it's at this is what it does but it doesn't actually call it so I can actually do something like X colon equals test and then call X now this will do the same thing as just writing the line test like that but I've just done it in kind of a convoluted way to show you that this is a valid thing I can actually assign a function to a variable this is a type function it's not a value I'm not printing hello when I do this but what happens now is that X is equal to this test function so if I call X by putting the parentheses like that it will call the test function think of it as a basic substitution right X is equal to test so let's put tests there for x boom call and that will work fine so if I do this and I run my code let's have a look at it here we can see that hello gets printed no problem nothing's wrong with this coat so that is a totally valid thing to do is assign a type function to a variable and then call that variable now if we went like this and we said you know X int and maybe we did something with X so maybe we just printed it out like that now what I would have to do is pass some value when I call X so this is totally valid I'm you know there's nothing wrong I save this file there's toast squiggly lines just when I call X now I need to pass the parameters that I would typically need to pass if I was just calling test so that is a way that we can actually store functions and what this means is first of all that I can assign any function to a variable I can actually create functions anywhere I want they don't need to just be in this kind of main line of the program that we've been looking at and I can return functions I can pass functions as input in as parameters I can do many different things with functions so let me show you an example of creating a function inside of a function so let's see function main is a function right it says fun totally bounded so now let's actually make a function inside of this and call it so what I'm gonna do is I'm gonna say test colon equals I'm just gonna say func like this and then go fmt dot print line in fact I could have just copied this I guess but we'll print hello just like we had in the previous example now what I've actually said here and this may look weird is that test is the variable that is storing a function that takes no parameters has no arguments does not return anything and simply prints hello and now if I want to call test I can do this and that's totally balanced let's save and see if that we got any squiggly lines here doesn't look like and if I run my code you'll see that we get hello popping up so this is a valid line of code too right now it may seem weird because here you did func and then you had to name it but when you do this inside of another function or you're assigning it to a variable what you can do is simply write it like this so the variable that you want to store this function is equal to some function this function doesn't have a explicit name we're just storing it in tests that returns nothing and prints the value hello now I could though put a parameter like index here and we could just print out X and that works totally fine that just means here now I got to put some values so say like five and let's run this now and we see that undefined X s or I wrote into X that should be X in my apologies okay so let's have a look at this now and we get the value five printing out to the screen so as a valid thing to do now because we can do this we can do many different things with functions we can do some pretty cool things actually so what I could do is actually um let's see if this makes any sense function func okay so let's do this I'm gonna do an example where this function returns something let's say function take some value X and what it does is simply return X multiplied by a negative one that will say that's what the test function is gonna do so it's gonna return some int and here what I'll do is return X multiplied by negative one okay so now we have the function test and it returns X multiplied by negative one now what I'm actually gonna do because I'm gonna call this function right directly from where I'm defining it so I'm actually gonna put eight in here and what this will do is it will now call this function because you can imagine it's not even attached to test right if we just put it like something like this and what it will do is call the function that we've just created with the parameter eight which will mean it will return e 2 times negative 1 and now store that value inside of test so if I actually go FMT oops FM t dot print line and I just print test like that you'll see that assuming I haven't made a large error here that this should work so let's run this go tutorial run go and we see we get negative eight so what actually happened to you is I made a function write that were takes a value and then returns a value and we called it directly in line with the parameter eight and since that happened all here instead of storing the function inside of test we stored the value that the function returned so why would you want to do that couldn't tell you off the top of my head but there is some examples and use cases of doing something like this but I just want you to really understand how the function works so this the function type I can call it by simply putting parentheses on the outside of it and that's no different than me just doing that right because again if you do the substitution well test is just equal to this function so it's no different than me just putting eight here because it's just the function right that I'm calling you can think of the substitution of tests with all of that so that's the idea behind that and a really cool thing that you can do with functions that you can call them like that and you can run them like that now let me show you how we can actually pass a function like test to another function so I'm gonna say func test - let's say that it actually takes as a parameter a function so what I need to do here if I want this to accept a function as I say like my func so I name the parameter whatever I want and then I have to do func I have to put the parameter type that it's gonna take in the parameter type that it returns so I define that this is actually the type of my funk because the function type is specific to what it accepts and what it returns so those need to match and then what I can actually say here is well I could return another function if I wanted to by writing func and then whatever it was going to return so maybe a function that took a string and returned it int this would mean I'm returning some function that does that or in this case I could just return nothing which I'm going to do so here we're saying test to take some function that takes an int and returns an int and what I'll actually do is just call that function with the value 7 so now we'll just see whatever function is that we pass we'll just call it and see what it does and in fact we can fmt to print line the result just so we actually get some kind of output here to look at so let's print the result of my func 7 and then down here let's go ahead and let's write the line to call test - so I'm gonna say test - and I'm actually going to pass it test so what this will do is it will pass this function that we've defined so the function that takes an int returns an int that's just x negative 1 we pass it to this function right so that gets stored in my function this test function and then we call that function with the value 7 and print the result so let's have a look at this I might have made a mistake here but let's run this and see what we get and no mistake we get negative seven so hopefully you understood how the flow of that worked so I actually passed the function I defined here to this function and this function called that function now what's nice about that is I can make another function here let's call it test3 why not colon equals func maybe will have it take some string ax and maybe it doesn't return anything maybe it just prints that out so we say FM t dot actually we'll have it return something let's do will actually make it the same thing otherwise this isn't gonna make sense what I have but K so it takes an int returns an int and maybe this time what it'll do is will return x x seven okay so that's what test3 does now all I have to do if we want to change this function so I want it to call a different one it's just put test3 here and now what it will do is it will call test3 with the value 7 so let's have a look at this and go go run tutorial go and you see well since this function isn't being used we have an error so let me just comment that out like that so that's a multi-line comment by the way if you do a forward slash Astrix and then forward slot and then asterisks forward slash that gives you a multi-line comment which comments out the entire section Robby you see we get 49 as that answer so it's a valid thing to do so you just have to understand that about functions that you can pass them around the program and that you can define them in line so I can make them equal to some variable and in fact I don't always have to do that but I could literally write a function and just call it immediately so if I have some function like let's do it here so I'll get rid of this one and we'll get rid of this here and I just write like func like that and maybe all we'll do in here is this fmt dot println test I mean this is a valid line of code to have and what this will do is just print test immediately because I just make a function and I just call it I didn't need to assign it to a variable it just got called right so that's a valid thing to do now I'm gonna show you the last thing which is returning a function and talk about the function closure which is the title of this video so let's make a new function let's say func return func standing for you will return a function and what will take is actually some value sure let's take X which is let's say it's a string this time and then we'll have it return some function they maybe just print something out right so we'll just say okay func like that's and what this is saying is we're gonna return a function that takes no parameters and returns nothing if I wanted it to take a parameter and return something I could do something like int int that would mean it takes the parameter that's an integer and returns an integer value so anyways we're gonna say just return anything what I'm gonna do is return a function that's simply fmt a dot print line of X so all this function does is prints the value X so we return that function from return func so now what I could do in here is say return func and then I'm actually gonna do a weird thing here I'm gonna call return func and I'm gonna call it again so what the reason this works and I know this seems really strange if you haven't seen something this complicated before is because what return function does and I have to give it a string sorry so let's give it hello is it return to function so this line here will be replaced with this function so you can imagine that all I'm really doing when I put the brackets there is just writing them right here so that means I'm just gonna call this function immediately after I get the return value from this return func and it'll print out hello so let's have a look at this let's go go run tutorial go and we get a low printing out totally fun now we could again we could use multiple times so I'd say hello and say you know goodbye like that no problem with that there we go goodbye and then of course I can also store this function in a variable so I can say something like X colon equals return func and then down here I could call X like that if I wanted to call goodbye and that would be totally fine as well perfect all right so that is pretty much how that works now this function is actually known as a function closure now the reason it's known for that is because it uses a value that we've defined outside of the function inside of it it's kind of hard to explain the function close your aspect of it but the idea is whenever you have a function inside of a function that uses some values from that function so in this case we're using the value X it's known as a closure and that's just kind of the way that you describe it if that's just the name for is just a function closure so we're saying this is a function closure it uses the value X now I could do something like this like some : equals zero and I could actually just print Ln some right and that would be totally fine I'm using the variable I defined here inside of this function which just makes it a function closure now there might be more to that explanation that I'm missing out here if you guys think I'm blacking and not definitely let me know and we can make another video about it if we have to but the term is not extremely important to understand it's just you understand the idea that we can use variables we define within the same function so within this out exterior function inside of this function and then I would be known as a function closure because we're using that variable and that was my understanding of it when I was reading through the goaline Docs although there might be some more than I'm missing up so now is that is pretty much been it for this more advanced function video I just wanted to show you what a function really is like we can pass it around we can put it in as a parameter we can put it in as a return value and just keep in mind that the type of a function is specific to what it returns in what parameters it takes so if you want to have a function that does something like takes an instant return int we need to make sure you specify that explicitly with int int okay so I think I'm in a wrap up video here if you guys enjoyed make sure you leave a like subscribe and I will see you in the next go line tutorial
Info
Channel: Tech With Tim
Views: 24,321
Rating: undefined out of 5
Keywords: tech with tim, golang tutorial, go programming language, go function, go function closure, function closures go, go programming tutorial, go programming tutorial for beginners
Id: vdm04bVzkLg
Channel Id: undefined
Length: 14min 33sec (873 seconds)
Published: Sun May 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.