Topics Covered:
For this project, you’ll use three inputs to affect three outputs. Each button will change the brightness of an LED. Since each input does the same thing for its respective output, we will use a function to save some repetition in the code.
Read the Code Walkthrough Text (click to open)
New Concept: Creating Functions
Functions are groups of code that run together. You use functions every time you write one of our programs- void setup() and void loop() are functions. When you're programming, you're filling out the contents of those functions.
When you use a function like digitalWrite() or tone(), you don't see the contents of the function, but you're using it by passing it arguments.
You can create your own functions in order to make your programs simpler. They're a labeled group of commands that you can use at any point in a program. If you find yourself repeating the same statements multiple times within a program, consider making a function out of those commands.
You should place your function outside of the void setup() and the void loop() functions. Put the function at the very top of your code.
The first word in creating a function is its type. A void function simply does the code between the function's curly braces- it doesn't send a value back to the code. If you have a function that does a math equation, you may create an int function that can send a number back to the code and be used by the rest of the program.
The second word in the function is its name. It's best to name a function clearly so that you know what its job is.
The third part of the function is to define the arguments that the function requires to work. Not all functions require an input- they simply do their job when called. It's up to you to decide what information your function requires. When creating an argument for your function, you define what type of value it will be - int is usually fine for numbers. Then you create a name for that argument. This is the name you'll use for that value throughout the rest of the function.
To require another argument, just type a comma, the next argument's type, and the next argument's name. You can require many arguments for a function to work, but it's recommended to keep the number low (3 or fewer) for simplicity. You can also create optional arguments, meaning that the argument will have a default value unless you call the function with that value changed. Optional parameters are a more advanced topic.
After you've defined the type, name, and requirements of the function, you type an opening curly brace and then start typing the code that your function will run when called. When you want to use the value of the arguments that the computer passed your function, you type the name of the argument that you put in the parentheses.
For example, if you had a function that set pinMode to OUTPUT for any number you passed it, you might have:
void pinOut (int _pinNum){
pinMode(_pinNum, OUTPUT);
}
That's not a very useful function (pinMode() is a function, too!), but it would work.
Functions can contain if statements, loops, variables, and calls to other functions. They can contain a program that takes an hour to run- anything you define in between the curly braces. Once you've defined what your function will do, you can close the curly braces.
To make your function run, you have to call it. That is, you have to tell it to run and give it any information it requires in order to work. To call a function, you type its name and an open parentheses, the value you want to pass the function, and then a closing parentheses and a semicolon.
A call to the pinOut function may look like:
pinOut(9);
There are multiple errors that can occur when you are using a function. For example, if you pass the function the wrong type of information, too much information, or too little information, it won't know what to do with that information and your code won't run.
Functions are very useful in breaking up more complex code into separate pieces that can run alone.
Quiz
If you're having trouble, try to run a program that can help you find the answer.1. When creating a function, where do you define what arguments the function will require?
2. When you call a function from the loop of your code, what code runs after the function has run?
3. When is a good time to think about creating a function?