Programming Fundamentals
Functions
12 min · 2 worked examples · 3 questionsFree
Packaging work into named, reusable pieces, the foundation of readable code.
After this lesson you can
- Write a function with parameters and a return value
- Say where a variable is visible and where it is not
- Break a long sketch into pieces you can name
A function is a named piece of work
You have already used functions: digitalWrite(), delay(), analogRead(). Someone wrote each of them, and you call them by name without caring how they work inside.
Writing your own has the same benefit. If you find yourself writing the same few lines in several places, that is a function waiting to be created. The name then documents the intent, so blinkTwice() reads as what it does, while four lines of digitalWrite and delay have to be decoded.
In the full lesson
- A function is a named piece of work
- Anatomy of a function
- Parameters and return values
- Scope: where variables live
Free with an account. Read it, work the examples, answer the questions, then build it in the simulator.