Level 2Unit A · Session 5
5

Unit A · From blocks to text

Your own functions

What we are making: a traffic light, written so that the main program reads like a description of a traffic light rather than a wall of pin numbers.

The idea

Last year, when a stack of blocks got long, you could wrap it up and give it a name with define. Then you used the name instead of the whole stack.

That is a function, and it was a function all along. In C++ you write it out yourself, and it is one of the most useful things in the entire language.

The block you already know
The line that does the same thing
define flashRed
void flashRed() {
turn on pin 11
digitalWrite(11, HIGH);
wait 0.5 seconds
delay(500);
turn off pin 11
digitalWrite(11, LOW);
}
flashRed
flashRed();
define becomes void. Using it is the name with round brackets after it.
  • void means “this function does not hand a value back”. Most of yours will be void.
  • The name is yours to choose. Say what it does: flashRed, stopMotors, readSensor.
  • The empty round brackets mean it needs no extra information to do its job.
  • To use it, write the name with the brackets and a semicolon. That is called calling it.
⚠ Careful
A function must be written above the place you first use it, or the compiler will say it was not declared in this scope. Put your functions above loop().

Why it is worth the trouble

Here is a traffic light without functions.

It works. Now say what it does, out loud, just from reading it.
1void loop() {
2 digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW);
3 delay(4000);
4 digitalWrite(11, HIGH); digitalWrite(12, HIGH); digitalWrite(13, LOW);
5 delay(1000);
6 digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, HIGH);
7 delay(4000);
8}

And here is the same traffic light with them.

The same program
1void set(int r, int a, int g) {
2 digitalWrite(11, r);
3 digitalWrite(12, a);
4 digitalWrite(13, g);
5}
6 
7void red() { set(HIGH, LOW, LOW ); }
8void redAmber() { set(HIGH, HIGH, LOW ); }
9void green() { set(LOW, LOW, HIGH); }
10 
11void loop() {
12 red(); delay(4000);
13 redAmber(); delay(1000);
14 green(); delay(4000);
15}

Look at loop() in the second one. Four lines, and it reads like the sequence of a real traffic light. That is the point of functions: the main program says what happens, and the fiddly details live somewhere else.

💡 Tip
void set(int r, int a, int g) takes three pieces of information, called parameters. They are the white ovals of a block — the numbers you dropped in. You do not have to use parameters this session, but you will want them by March.

Wire it up

3
LEDs — red, yellow, green
3
Resistors 220Ω
4
Jumper wires
+−+−jihgfedcba151015202530+++111213GND
Three LEDs, three resistors, one shared ground rail.
Pins used111213GND

Try it

  1. Build the version with functions. Get it running.
  2. Add a pedestrian() function that flashes green three times, and call it after green.
  3. Change how long green lasts. Notice you touch one number, in one obvious place.
  4. Hand the code to your teammate and ask them to read loop() out loud. If they can say what it does without asking you, your names are good.
What you seeWhyWhat to do
'flashRed' was not declared in this scopeThe function is written below where you use it.Move it above loop().
expected initializer before ...A missing bracket in the function's first line.It is void name() { — all four parts.
It compiles but nothing happensYou wrote the function but never called it.Defining it is not using it. Add flashRed(); inside loop().
It runs once and stopsYour code is in setup() rather than loop().setup runs once. loop repeats.

What you learned

  • A function is last year's define block.
  • void name() { ... } to write it, name(); to use it.
  • Functions must appear above where they are called.
  • The main program should read like a description, not a list of pin numbers.
  • Good function names are verbs.
Challenge optional — only if you finish early
  • Write a function that takes a number and blinks that many times.
  • Then call it three times with different numbers.
4. Loops