Functions in Arduino
Free Arduino Course
An Introduction to Arduino Functions
Become the Maker you were born to be. Try Arduino Academy for FREE!
Arduino Functions Video Transcript
Functions in Arduino. From a bird’s eye view, A function is a self-contained unit of program code designed to accomplish a particular task.
Think of functions as building material for Arduino programs. Just as your house is made of wood, drywall, and other materials Arduino programs are composed of different parts. These parts are functions.
If you’ve programmed in C at all before, you’re likely familiar with the function main() which every C program needs to have.
If you’ve programmed Arduino before, you probably know that every sketch, or Arduino program, needs to have setup() and loop()
Since Arduino is based on C, main() is there, it’s just hidden.
Arduino comes pre-packaged with many different functions for handling different common tasks. Another cool thing about functions is that you can write your own custom functions to do whatever you want. We’ll talk More about that in another tutorial.
First, they save you from repetitious programming. If you have to do a certain task several times in a program, you only need to write an appropriate function once. The program can then use that function wherever it needs to, or you can use the same function in different programs.
Experience has shown that the best way to develop and maintain a large program is to construct it from smaller pieces. Each of these pieces is more manageable than the original large program. These pieces are functions.
Using functions is a good idea because it makes a program more modular, hence easier to read and easier to change or fix.
Programmers can write functions to define specific tasks. Not only can these tasks find use in multiple places in the program, they can also find use in completely different programs altogether. This cuts down on repetitive work and duplicating the same code over and over again for different programs. For example, if you want to display something on the screen you use the Serial.print() function (remember that the name before the dot is the library and the actual function comes after the dot).
Sure, you could write a bunch of code from scratch to print some characters on your screen, but why reinvent the wheel (at least in this case)? This has already been done and can be a huge time saver. Instead of writing a bunch of lines of code to handle the low level operations of writing a character to the screen, you can just call (or invoke) the function that does this. Way easier.
let’s dissect an example function, Serial.print() and talk about the different parts. We’ll use the following code snippet and focus on the function.
void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: Serial.print(255, BIN); Serial.print("\n"); }
First, we need Serial.begin(9600) to get an output on our screen with the serial monitor. I talked about the serial monitor in another tutorial, so that’s all I’ll say about that here.
Next, we see this particular function has two parts with a period or dot in between them. Not all functions in Arduino are like this (such as delay()) but many of them are. For the ones that are, the general format is Libraryname.functionname where the library name is capitalized, and the function name is not. The print() function is part of the Serial library, so that’s why we need both parts to use.
What about the stuff in the parentheses?
These are the function’s arguments, which you can think of as the “stuff” the functions uses.
The \n inside the quotes in the second in the second print statement tell the computer where on the screen to print the characters and how to print them. In this case, /n tells the compiler to print the text on a new line. There is another way to do this, for illustrative purposes we’ll stick with this way for now.
The first print statement has 2 arguments in it separated by a comma.
First, we tell the compiler to print the number 255. The BIN tells the computer to print it in binary. Let’s demonstrate. As we can see here, 255 in binary is 8 ones.
Usually, you’ll use just one argument for the Serial.print() function. The point I wanted to illustrate here is that some functions can take more than one argument, while others, like loop() and setup() take none.
Some functions return a value.
What do we mean by return?
Sometimes, a function will some stuff, get a result, and return that result to the main program or the function that called it.
Let’s use this fictitious function that adds 2 numbers as an example.
int Numberadder(num1, num2);
The int in front of the function name indicates that it returns an integer. In this case, that integer is the sum of num1 and num2, which are the things the function takes in or uses.
Earlier I said some functions return a value. Others do not (like delay() for example). You may see functions that do not return a value written like this: void Functionname()
The void simply means the function doesn’t return anything.
Other functions return nothing and take no arguments. You may see these written like: void Functionname(void).
Again, the void in the parentheses means it takes no arguments.
Arduino Functions as a Black Box
It’s pretty obvious how our fictitious example function works, but often times it’s not so obvious.
For now, a good to think of a function is as a black box that we can’t see inside of.
This box takes some inputs, and then has some sort of output.
We don’t know exactly what goes on inside the box and we don’t care, as long as it works.
When we talk about writing our own functions in a later tutorial, we’ll take a peek inside the box, but for now we don’t know what’s in there or how it works.
Quick Recap
- A function is a self-contained unit of program code designed to accomplish a particular task
- Arduino comes pre-packaged with many different functions for handling different common tasks
- They save you from repetitious programming
- They’re composed of different parts (What the function returns, if anything, name, and arguments it takes)
- For now, a good way (or abstraction) to think of a function is a black box with an input and an output. We don’t need to know what’s in it or how it works.
Become the Maker you were born to be. Try Arduino Academy for FREE!