How to Make Your Own Functions
What Is a Function?
A function is a specific instruction that an Arduino can follow. In Arduino programming, you may find it useful to create your own functions. The function digitalRead() tells the Arduino to read the pin you select and tell you if it is HIGH or LOW. When you create a function, you are really grouping together several instructions into one. For example, you could create a function to blink an LED given the time between turning the LED on/off.
How Do I Create One?
In order to declare (create) a function, you will need to write a function declaration. Function declarations should go below void loop() on the outside of the curly braces. Here is what a function declaration will look like:
returnType functionName(dataType parameter1, dataType parameter2...){
//The actual code will go in here, within the curly braces.
//This is called the function body.
//...
}
Let's break this down...
Parts of a Function Declaration
Function Name:
What do you want to name your function? This is the unique title of the function that you will type every time you want to use it. If the function will blink an LED, you could name it “blinkLED”. You could also name it “function”, “hamburger”, or anything else you like.
Parameters/Arguments:
What values does your function need to know in order to operate? What is the data type of each value (int, long, double, string, char, etc.)? Whenever you use the function, you will put these values that the function will use within the parentheses (). If you are blinking an LED, you will need the pin of the LED (int) and the time between blinks (int).
returnType:
What is the datatype of the value that will come out of your function? If you are blinking an LED, there probably will not be a value that is returned/output by your function. If this is the case, the return type will be void. If you do indicate a return type, you will need a return statement somewhere in your code. The return statement is the code that actually outputs the value. A return statement is modeled like this:
return value;
A return statement goes inside the braces of each function you want to declare.
Below is an example of a function that will blink the LED that is connected to the pin ledPin for blinkTime milliseconds:
void blinkLED(int ledPin, int blinkTime){
digitalWrite(ledPin, HIGH);
delay(blinkTime);
digitalWrite(ledPin, LOW);
delay(blinkTime);
}
Here’s how we could use the function in the code:
for(int i = 1, i != 5, i++) //This code will repeat 5 times
{
blinkLED(2, 200);
delay(i*200);
blinkLED(2, 200);
}
Notice how we didn’t write ledPin or blinkTime when we used the function. Those two names are just what the function calls the numbers we put into it. Also notice how we can either put a number in, like we did for the ledPin, or a variable, like we did with delay. This code will make the blinking slow down every three times until it comes to a stop.
And here is an example of a function that takes in a number and adds two to it:
int addTwo(int num){
//we use int here because the program “spits out”(returns) a number
return num + 2; //this tells the function to “spit out” num+2, making the function equal that value.
}
Here's how we could use this function in some code:
int score=6;
cheaterScore=addTwo(score);
Serial.println(cheaterScore);
Using functions you wrote works the same as using the built-in functions. You call the function by typing the function name, then each part of the argument within parentheses. For example, if you wanted to blink an LED connected to pin 2 for 500 milliseconds, your overall code would look something like this:
void setup(){
pinMode(2, OUTPUT);
}
void loop(){
blinkLED(2, 500);
}
void blinkLED(int ledPin, int blinkTime){
digitalWrite(ledPin, HIGH);
delay(blinkTime);
digitalWrite(ledPin, LOW);
delay(blinkTime);
}