LESSON 34: Return a Variable Value from a Function in Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 34 on learning to program the most excellent Arduino microcontroller what I need you to do is pour yourself a nice strong cup of coffee and get ready to learn a little bit more about functions okay what we're gonna learn today is how to return a variable value from a function okay this series of lessons really started in Lesson number 32 where I showed you how to start using functions in Arduino and the idea behind a function is to take a complicated task and break it down into logical chunks and then to define code or functions to do those logical blocks and then your main program is much easier you just call these blocks and execute them and you can start doing things in a more modular fashion but once you start doing that you have to start being very mindful of the difference between global variables and local variables so one way you can do it is just make everything a global variable all the functions know what all the variables are and do it that way but what we showed in lesson number 33 is is that as we start doing more sophisticated programming we really need to start using local variables where the variables are local to the individual functions and we pass values back and forth and so what we're going to just look at today is we're gonna start looking at our first look at using local variables and how we can pass a value back from a function up to the main void loop so if you haven't watched lesson number 32 I really suggest that you kind of get up to speed on what we were doing there and what you can do is you can go back to what we did in Lesson number 32 I will first get out of your way and then I will go here and this is the program that we wrote on lesson number 32 if you don't have this code if you don't have this code you can go back to the most excellent wwth wacom go to Arduino lessons go to Arduino lesson number 32 and I have the code there for you to use but what this code is it's two average grades and just a quick recap of what we did we defined all of our variables to be global variables up here and then we just defined in our loop functions or we called functions that were logical blocks of how you would average great so if you wanted to average a list of grades what's the first thing you would need to do what you would need to input the grades and then what would you need to do you would need to average the grades and then what would you need to do you would print the grades okay so this is very logical input grades average grades print grades and that is your void loop then you come down here and you define these void functions of input grades and then you define the function average grades and then you define the function print great so you see all the sudden starts looking really cool really modular but the way we did this is we did this with global variables that means we define the variables up here and all of the functions knew what all of the variables were but we're going to start moving into local variables all right and so let's think about that let's think about what we're really going to do is we're gonna make average a local variable and so we're not going to define it up there but what I'm going to do is I'm going to define it down here so I'm gonna say float and I'm going to define average and now who's the only one that's gonna know what a V is the only one that's going to know what a V is is between this curly bracket in that curly bracket because AV is local only to this and I'm just going to take that one out and then what I'm gonna do here is just say after I calculate average I'm going to do a serial dot print L in and then I'm gonna say make it a print say your average is and then I'm gonna do a serial dot print L in I'm gonna say AV okay so this print statement will be happy because AV is inside its defined inside this clause inside this void loop and so it's a local variable but this print statement is part of the same chunk of code where I define it so this should work very well but now what we're gonna do is instead of just calling F grades like this what we're going to do is we're going to say that ad is equal to the function well if we make it equal to the function the function needs to have a command in it where it returns something back up to us so rather than just have global variables up here we're gonna start passing things back and forth and what are we going to pass up well AB great so when we come down to AB grades before we just calculated F what's the problem here it's not going to know what AB is because AB was not defined up here and it is not defined in AB grade so it's not going to know what it is and we're going to use a different variable what I'm going to do is I'm going to define one it's gonna be float and we're gonna call it average right and then we're gonna calculate average now what's the problem this is gonna know what average is but up here it's not going to know because I've got local variables here local variables here so when I have chunks of code using local variables this is the first way that I'm going to teach you how to pass a parameter back up and so what I'm going to do down here after I calculate average I'm gonna say return average okay so what happens this function have grades is going to declare a variable average and then it's going to calculate average and what's it going to do it's going to pass it back up and then who's going to catch it it's passed up to here so AB is going to catch it AB is equal to this well what comes here whatever you return what am i returning I'm returning average now you know how in the earlier lesson 32 I just put a void in front of your a void in front of everything so like if you define a function you just define it as void that's what you do if you're if you're using global variables but now I'm returning something so what do I have to do when I define the function I've got to define what type of variable I am returning what am i returning I'm returning a float so now this is still the command that defines this function but it's not void it's not empty it's returning something what's it returning it's returning average when average pops back up here who catches average the variable a V okay so this AB grades will go down to AB grades it sees it's going to create a float which float does it create it creates average and returns that you see sort of like passing the football I pass it and you catch it okay that's how this thing works so let's see now if this thing will work let's hit it here oh it's not happy what happened oh oh oh oh okay I see this print grades which using average as well and so what we are gonna do is we are just going to get rid of this we're just going to get rid of that whole function does it make sense that when I did not declare AB as a global variable the other functions that wanted AB were not happen so we just got rid of that okay I think everybody is happy how many grades I have three grades input the first grade 97 98 and you guys are most exceptional students I think you would probably so we're gonna say a hundred your average is ninety eight point three three boom okay now let's just go back over this and let's make sure that we understand what happened Ave is now a local variable known only to the void loot Ave is equal to whatever the function have grades returns when we go down to AB grades now it's returning something so it's not void its returning something it's returning a float so I define the function with float AB grades and then I have to declare the variable that I'm going to calculate which is average I calculate average and then what do I do I return average what does that do that comes up here and puts that value here which then is caught by a V the local variable okay does that make sense let's just start with another one just to understand how to do this return just just from scratch I'm going to try to do just a really quick one so let's come up here and say file new and what I want to do is let's just create a function that'll square another square a number okay and let's use local variables and so what I'm going to do here is we always need our trusty serial monitor so serial dot begin 9600 now we're going to come down here and we're gonna define X so we're gonna say let's make it a float not a cattle float float X and then I'm gonna get X from the user how do I get something from a user I ask I wait I read and so I'm going to say serial dot print L in what number do you want to square like that let me make this big so what number do you want to square being diligent to put oh man I messed up for you guys you only get me up here I didn't do the shift that's most of them where I hit the caps lock okay what number do you want to square I asked what do I do I wait so I'll say Wow serial dot they all equal equal zero what do I do absolutely nothing so an open and a closed Clause open and close curly I forget that it puts those clothes in there for me so I wait and then what do I do read so I'm going to say X which is what I'm waiting for is equal to serial dot parse float so now I read X now what do I want to do I want to say X 2 which would be like x squared is equal to what we're going to call it a function x squared now let's think I have a new variable here I better define it I'm using local variables so I'm going to define it here a fellow 80 and then I'm going to say x squared so now I've just I've declared my variables and X 2 is equal to x squared and then serial dot print I'm going to put a series of things in here okay let's put in X just to print out X and then say serial dot print and then put in the word squared is equal to and then serial dot println and then put X 2 so what should this do what should this little thing of code do it should ask you for a number and then it should square it and then it should say that that number squared is equal to and then give you the answer now what is the situation that we have here well we haven't taught it what x squared is so we've got to define the function where do we define the function less than 32 we define it after after the void loop that means after the whole void loop has ended so now we're going to define it remember what we learned eat earlier this lesson is it avoid no it's not avoid because I'm gonna pass x squared back up to it so what we're gonna do is x squared is afloat so we're gonna call the function a float function and we're going to call it the x squared function and then x squared is gonna be a void and then we're gonna open curly and then it should close the curly for me there it is now what does this do it's gonna have to know what I it's gonna have to know what X is so how is it gonna know what X is X is a local variable so I couldn't just say like float answer I couldn't just say float answer because if I said float answer and then I say X is equal to if I say X if I say answer is equal to x times X what is the problem well the problem is it doesn't know what X is because I made it a local variable so I've got to get this X down here somewhere how can I do that well I gotta pass it inside of here so what am I going to pass I'm going to pass X now if I pass X I've got to catch it here and when I have to define it again so I'm going to say float X so this X here this local variable X is passed within these parentheses down to this X which is going to catch but this X is different than attics but it passes it now answer is equal to x times X this x times X now what do I need to do what I need to do now is I need to return what I'm going to return X then that's going to come back into x2 you think this is gonna work how many mistakes did I make that I don't know about let's see that wants to save it okay looks like it might be happy what number do I want to square well I want to square 2 2 squared 2 squared is equal to 2 what went wrong well it's good time sometimes to have a problem for the space in there X 2 is equal to x squared and then I print X 2 and then x squared float answer Oh what did I return did you catch that I returned to X back what did I want to return answer did you guys catch that I hope you caught that okay what number do I want to square to 2 squared is 4 boom I do want a space in there so I'm gonna put a space right there let's try that again do you see what we just did we're starting to do modular programming we're starting to do modular programming with local variables what numbers do you want to square 2.5 okay 2.5 squared is equal to 6 point 2 5 hey that looks good all right so what you can see is is that once we start using local variables we have to be mindful of passing values down to the function and then returning values from the function and so really what I wanted you to see today is the object of today's lesson was to see that if you're going to return something when you define the function you don't define it as boy you define it as a float or an INT or a string or whatever you're returning you define it it's whatever you are returning if you are just using global variables you can make all your functions called void void void but if you're returning something you need to define the function using the type of variable that you are going to to return and then you can see that we return answer and answer was a float so therefore this has to be a float so really the purpose of this was to understand how to return something and then to have a variable up in the call that is just equal to what you call and then doesn't this kind of start getting excellent x squared is equal to x squared of what number of X and then that's passed down I'm gonna do another video really the purpose of this video is to learn how to just return something to use the return function but in the next video we're going to learn more about passing parameters to and from functions inside the parentheses does this make sense I would love to hear from you guys I would love it if you would leave a comment think about giving us a thumbs up subscribing to the channel okay because I think this is really important stuff and we've done a whole lot of lessons and we're a little late to get to this but this modular programming is really important and starting in Lesson number 32 is where we're really getting into this modular programming lesson thirty-five I'll talk to you more about passing parameters back and forth palma quarter top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 40,248
Rating: undefined out of 5
Keywords: Arduino, Functions, Variables, Return
Id: sqcVxIQQewc
Channel Id: undefined
Length: 19min 21sec (1161 seconds)
Published: Wed Aug 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.