Lesson 10 | Functions Overview | Arduino Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're doing fantastic in this lesson we're going to do an overview of functions now this is going to be a general discussion about functions on how to use them so really what we're trying to do here is lay out a framework for understanding how functions work and how we can use them specifically in this lesson what we'll be talking about our function calls function parameters passing arguments to functions and then finally about different ways that functions work alright let's get started do you recall that we said functions are like the verbs of a programming language so if you want to get something done it's probably going to involve a function now the Arduino programming language which again is essentially the same as C++ and a lot like C the C programming language is just chock-full of different types of functions that do all different types of things for us if you can think of something there's probably a function that does it now to use this grand variety of functions all we have to do is type the name of the specific function that we want to use that's it you just type its name and then you follow its name by an opening and closing parenthesis and then sometimes you have to put values in between those parenthesis so let's take for example this function called digital right so what the digital write function does is it wants to know a PIN number on your Arduino and then it wants to know a state either high or low and once you give it that information what it will do for you is apply either 5 volts which would be a high state or zero volts which would be a low state so when we type this on you know in our program when we type digital write that is called a function call and that's just some terminology that you might hear and we'll be using throughout the course and that's why I want to bring it up we'll be going over a lot of terminology in this lesson so like we just talked about some functions require information from us in order for them to work so this function digital right it needed to know two things it needs to know the pin number that you want to write voltage to and then it needs to know the voltage that you want either high which is five volts or low which would be zero volts so this information the information that the function needs to operate are called the functions parameters now you might be wondering well how the heck are you supposed to know what the parameters of a function are and that's a really good question because when you look at the word digital right there's nothing in that that's going to tell you what the parameters are that this function takes in order to know what the parameters are you have to look that up in a resource called the reference so the reference is sort of like a user manual for programming languages now for functions that you use all the time like digital right you're just going to become familiar with what the parameters are so you won't have to look it up all the time but for functions that get new you know they are new to you you'll probably have to refer to the reference frequently it's really easy to use and you'll learn a lot more about it through the course enough about that for now so let's say this again the parameters are what a function wants or expects but the actual values that you give to the function so in this line of code where we've got the number three and the state high in there those specific values that we give the function are called the arguments so this might sound like a case of semantics to you but it's actually quite different so again parameters are kind of like the general definition of what the function expects the function in this case it needs a pin number and it needs a state the argument is the specific value that you pass to a function so these specific values that I send to the function are called arguments and when I give a function arguments it's called passing as in passing gas so I pass arguments to a function okay so let's do a quick recap when we use a function it's called a function call so we're said to be calling a function and the information a function needs to operate are called its parameters now the actual values we send into the function are called arguments and we when we sin those arguments were said to be passing the arguments so we pass arguments to a function proof that's a lot of terminology and keep in mind that's that's all it really is it's just terminology but the reason I want to talk about it here is because we're going to be using these terms throughout the course and I'm sure you'll hear these terms elsewhere so I think if you understand these terms it will help us communicate better okay so now let's talk about different ways that functions can do stuff for us so some functions like digital right all we have to do is send them some arguments and then they do something for us so you know we passed the number thirteen and hi to digital right and digital right goes out and it applies five volts at pin 13 so the function actually does something physical for us other functions return values for example the function digital Reed monitors a pin on the Arduino and lets you know the state of the pin either high or low so digital read it has one parameter and it wants to know the number of the pin that you want to check the state of so to make sense of this let's look at this line of code for an example we've got pin state equals digital read of three so what this line of code does is it assigns the output of the digital read function to the variable pin state so when this line of code gets executed the function digital read gets called and then we've passed at the value three so again three is our argument and so what digital read does is it goes and checks the state of pin three so it's key they're going to be high or it's going to be low and then what it's going to do is send that value back to our program now when it sends that value back it's called returning the value so the function digital read returns the state of that pin then in this case that value that gets returned it's going to get assigned to the variable Penn State another example would be a Celsius to Fahrenheit converter function it might take a number in Celsius and then return a different value a converted value in Fahrenheit so it returns the Fahrenheit value so if you want that value that new Fahrenheit value you need to assign it to a variable alright so far we've been talking about functions that take parameters in order for them to work you have to give them some information but other functions they don't need any information to work so one common example is the Millis function so when you call the Millis function it's going to go and it is going to pull the number of milliseconds that the current Arduino sketch has been running so Millis uses hardware that's integral to the integrated circuit that the Arduino uses and it's going to pull information behind-the-scenes you don't have to do anything and it's going to return this value to you and there's other functions that do this you don't have to give it any information it goes out it does stuff and it gives that information back to you so in this line of code the variable timing will be assigned the number of milliseconds that the Millis function returns okay now finally there's some functions that don't take parameters and don't return values they just do something when they're called so maybe you're working on a project that waters plants at different time intervals so you might have to make a function that simply opens a valve so in this line of code we've got open valve so the open valve function it doesn't take any parameters it's simply going to open a valve that we specify elsewhere now when we use a function it's important to realize that there's more than meets the eye for example do you know what DNA stands for it stands for deoxyribonucleic acid that's kind of a mouthful to say so instead of saying deoxyribonucleic acid people just shorten it up and say DNA DNA is an acronym it's a quick and easy way to communicate deoxyribonucleic acid without actually having to say deoxyribonucleic acid so functions are sort of like this so for every function name that you use like digital right or digital read there is a function definition that gets executed for you behind the scenes the function definition spells out exactly how that function is going to behave what parameters it will use whether or not it will return any values and all the stuff like that so in most cases you're going to care very little about the function definition you're going to be more interested in how to use the function so the power of a function is its ability to reduce your workload so programming languages are filled with tons of useful functions some used far more frequent frequently than others and what you'll find is you're going to get quite familiar with a couple dozen or so functions that allow you to do a whole lot and then other functions you'll kind of learn about them slowly and integrate them into your knowledge so later in the course you're even going to learn how to write your own function definitions so that you can make your own handy user-defined functions but that's going to be for a later time all right so let's review what we've talked about we talked about what it means to make a function call we talked about function parameters the parameters are the general values that a function expects in order to work we talked about passing arguments to functions the arguments are the specific values we give a function and when we give them to a function it's called passing finally we talked about different ways that functions work some take arguments and then do something some take arguments and then return a value some don't take any arguments and return a value some don't take any arguments and don't return a value but they do do something all right well hey you know we talked a lot about terminology in this lesson and just kind of again an overall framework of how we're going to be interacting with all different types of functions throughout the course all right I look forward to seeing you in the next lesson bye
Info
Channel: Programming Electronics Academy
Views: 39,279
Rating: 4.9406528 out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, crash course, arduino functions calling
Id: DrDB4oYtJrA
Channel Id: undefined
Length: 11min 9sec (669 seconds)
Published: Tue Jun 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.