Functions
Give a chunk of code a name, and your project suddenly becomes readable.
In 10 seconds
- • A function is a named chunk of reusable code.
- • void means it gives nothing back.
- • Parameters let one function handle many cases.
A function is a named bundle of instructions. You write it once, give it a sensible name, and from then on you can run the whole bundle just by saying its name. You have been using functions since your very first sketch — digitalWrite() is a function somebody else wrote for you. Writing your own is the moment your code starts to feel organised instead of piled up.
Why bother?
Look at the obstacle-avoiding car project. Without functions, every turn would need four digitalWrite lines and two analogWrite lines, scattered through the program in six different places. With functions, the code says forward();, left();, stopCar();. Anybody can read that and understand the robot's behaviour instantly, even without knowing which pin does what.
Three concrete benefits, in order of how much they will help you:
- Readability. Names explain intent.
readDistance()tells you more than five lines of pulse timing. - No repetition. Fix a bug once, and it is fixed everywhere the function is used.
- Testing. You can check one function works before building the rest on top of it.
Writing one
A function definition has four parts: what it gives back, its name, what it needs, and the block of code. If it does not give anything back, the return type is void. If it needs no information, the brackets stay empty.
void blinkTwice() { ... } is a function that takes nothing and returns nothing — it just does something. int readDistance() { ... } gives back a whole number, so you can write int d = readDistance(); and store the answer.
Parameters make functions flexible
Instead of writing blinkRed, blinkGreen and blinkYellow, write one function that takes the pin and the number of blinks as parameters: void blink(int pin, int times). Now blink(9, 3) flashes pin 9 three times and blink(10, 1) flashes pin 10 once. One function, endless uses.
Parameters are just variables that get filled in when you call the function. They exist only inside it, which keeps them safely out of the way of the rest of your program.
Returning a value
The word return sends an answer back and ends the function immediately. A temperature function might do the analogRead, do the voltage maths, and return a clean number in degrees. The rest of your program then never has to think about volts at all — it just asks for the temperature. Hiding messy detail behind a friendly name is what good programmers spend most of their time doing.
A simple rule of thumb
If you can describe a piece of your code in a short phrase — "check if it's dark", "open the door", "play the win sound" — that phrase is the name of a function waiting to be written. Try it on your next project: write loop() first as a list of function calls, as if the functions already existed, then go and write them. It is a surprisingly satisfying way to build.
Quick quiz: Functions
3 questions to check it stuck.
Next lesson
🧠 What is Arduino?
Ready to try it for real?
Reading is good. Wiring is better. Pick a project and put this to work.
Explore projects