You are on page 1of 3

Functions

In this lesson we're focusing in writing your own functions. The reason you need to
be able to write your own function is that as sketches start to get a little complicated,
then your setup and loop functions will grow and grow until they are long and
complicated and it becomes difficult to see how they work.

What is a function?
A function is a little like a program within a program. You can use it to wrap up some
little thing that you want to do. A function that you define can be called from
anywhere in your sketch and contains its own variables and its own list of
commands. When the commands have been run, execution returns to the point just
after wherever it was in the code that called the function.

By way of an example, code that flashes a light-emitting diode (LED) is a prime


example of some code that should be put in a function. So let's modify our basic
"blink 20 times" sketch to use a function that we will create called flash:
const int ledPin = 13;
const int delayPeriod = 250;

void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  for (int i = 0; i < 20; i ++)
  {
      flash();
  }
    delay(3000);
}

  void flash ()
{
    digitalWrite(ledPin, HIGH);
    delay(delayPeriod);
    digitalWrite(ledPin, LOW);
    delay(delayPeriod);
}

So, all we really done here is to move the four lines of codes that flash the LED from
the middle of the for loop to be in a function of their own called flash. Now you can
make the LED flash any time you like by just calling the new function by writing flash
(). Note the empty parentheses after the function name. This indicates that the
function does not take any parameters. The delay value that is uses is set by the
same delayPeriod variable that you used before.

Parameters

When dividing your sketch up into functions, it is often worth thinking about what
service a function could provide. In the case of flash, this is fairly obvious. But this
time, let's give this function parameters that tell it both how many times to flash and
how short or long the flashes should be.
const int ledPin = 13;
const int delayPeriod = 250;

void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop()
{
    flash(20, delayPeriod);
    delay(3000);
}

  void flash (int numFlashes, int d)


  {
    for (int i = 0; i < numFlashes; i ++)
    {
     digitalWrite(ledPin, HIGH);
     delay(d);
     digitalWrite(ledPin, LOW);
     delay(d);
    }
 }

Now, if we look at our loop function, it has only two lines in it. We have moved the
bulk of the work off to the flash function. Notice how when we call flash we now
supply it with two arguments in parentheses.

Where we define the function at the bottom of the sketch, we have to declare the
type of variable in the parameters. In this case, they are both ints. We are in fact
defining new variables. However, these variables (numFlashs and d) can only be used
within the flash function.

This is a good function because it wraps up everything you need in order to flash an
LED. The only information that it need from outside of the function is to which pin
the LED is attached. If you wanted, you could make this a parameter too -- something
that would be well worth doing if you had more that one LED attached to your
Arduino.

Another example of using the Functions and Parameters is in multiple LED’s

void setup()
{
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}
void loop()
{
  blinkLED(13, 5000);
  blinkLED(12, 250);
}
void blinkLED(int pin, int duration)
{
  digitalWrite(pin, HIGH);
  delay(duration);
  digitalWrite(pin, LOW);
  delay(duration);
}

You might also like