Start Reading

Functions: Let's make programming Arduino as easy as possible: Learn Programming and Electronics with Arduino

0 page


Discussion: In this lesson, we're going to do an overview of functions.  This will be just a general discussion to lay out a framework for understanding how functions work and how we can use them. More specifically, we'll be talking about: Function Calls Function Parameters Passing Arguments Different Ways Functions Work In an earlier lesson, we said functions are like the verbs of a programming language.  If you want to get something done, it's probably going to involve a function. Function Calls The Arduino programming language is essentially the same as C++ and a lot like C.  So, it’s chock-full of different types of functions that do all kinds of things for us.  If you can think of something, there's probably a function that does it. 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. Then you follow its name by an opening and closing parenthesis.  Sometimes, you have to put values in between those parentheses. Let's take this function called digitalWrite, for example.  The digitalWrite function wants to know a pin number on your Arduino, and then it wants to know a state, either HIGH or LOW. Once you give it that information, it will apply either five volts, which would be a HIGH state, or zero volts, which would be a LOW state. When we type digitalWrite in our program, that is called a function call.  I want to make sure that you’ve heard that terminology, function call, because you’ll be hearing it quite a bit.  We'll actually be going over a lot of terminology in this lesson. Function Parameters Some functions require information from us in order to work properly.  The function digitalWrite needed to know two things.  It needed to know pin number and high or low voltage. The pieces of information that the function needs to operate are called the function's parameters. You might be wondering, “How am I supposed to know what the parameters of a function are?”  That's actually a really good question because when you look at the word digitalWrite, there's nothing in the name that alludes to what parameters it needs. In order to know what the parameters are, you have to look it up in a resource called the reference.  The reference is like a user manual for programming languages. For functions that you use all the time, you'll become very familiar with what the parameters are.  You won't have to look it up all the time. However, for functions that are unfamiliar, 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 throughout the course. Passing Arguments Back to our discussion on parameters.  As said before, parameters are what a function wants or expects. The actual values that you give to the function, though, are called the arguments.  In our digiWrite example, we have the number three and the state as HIGH. Those specific values are the arguments.  This might sound like a case of semantics to you, but it's actually quite different. Parameters are just the general definition of what the function expects, like pin number or state.  The argument is the specific value that you pass to a function, like 3 or HIGH. One more term to learn is “passing”.  When I give a function arguments, it's called passing.  I pass the arguments to a function. Okay, so let's do a quick recap.  When we use a function, it's called a function call because we're calling a function.  Seems straightforward, right? The information a function needs to operate are called its parameters.  The actual values we send into the function are called arguments.  Lastly, when we send those arguments, we say we’re passing the arguments to a function. Phew, that's a lot of terminology!  Keep in mind that that's all it really is.  It's just fancy computer programming words. The reason I want to talk about it here is because we're going to be using these terms throughout the course, and

Read on the Scribd mobile app

Download the free Scribd mobile app to read anytime, anywhere.