Intermediate 6: Void Functions
In this lesson we will learn about non-value returning functions (void functions), why they are useful and how to use them. 



We learned about functions a bit in our earlier lessons.  Functions are great ways to save time and energy by storing commonly used blocks of code as named sets.  Throughout our lessons, we’ve used dozens of void functions like CoDrone.AutoConnect() , CoDrone.Control() , CoDrone.begin() , and many more.  Here we will finally learn how to write our own functions.



Recap: What Is A Function?

We talked about this briefly in Lesson 2: Pairing.  A function is a set of statements that can be labelled and called by name.

For example, to tell someone how to make a sandwich, we would have to say:

  1. Get a slice of bread
  2. Spread mayonnaise on the bread
  3. Place some ham on it
  4. Place some cheese on it
  5. Place some lettuce on it
  6. Finish it with another slice of bread

Instead of saying all of this each time you want a sandwich, instead we can just name these steps as “Make a Sandwich”.

Next time we want to make a sandwich, we can now just say “Make a Sandwich”.



That’s what a function does in programming too.  If we have a set of commands that we are going to end up repeating a lot, we can just set them aside and give them a name.  Now, whenever we want to run those commands we can just call out the name.



What’s a Void Function?

There are two types of functions

  1. Void Functions
  2. Return Functions


A void function is any function that does not have a return or output.   This means that the function does something, but it doesn’t give you any values to use.

This performs a bunch of commands, but we aren’t given a number to use.


return function is a function that gives you a value.

digitalRead()  returns a 1 or a 0 depending on whether or not the sensor 11 is covered, making it a return function. We will learn more about return functions in later lessons.



Writing Functions

There are two parts to writing a function

  1. The Declaration
  2. The Call


The Call

Calling the function is easy.  A ‘call’ is just using the function.  For example, to call the function CoDrone.Control , we would write

Easy enough.


The Declaration

Before we call a function, we have to declare it.  This is where we tell the program what we want the function to do.

Writing a Function

return data type FunctionName(inputs) {

return data type – This is where you declare the data type of your return value

Function Name – The name of the function

inputs – The input parameters that you will use in the function

statements – The code that the function will run when called


The best way to learn how to write a function is to practice, so let’s start with an example.


Example 1: GoForward()

To make your CoDrone fly forward for 2 seconds, you would write

Let’s turn this into a function called GoForward()

Now every time we use the line GoForward() , you can imagine it being replaced by our function.

This will make our CoDrone fly up for 2 seconds, pitch forward for 2 seconds, then land.

Notice that the declaration for GoForward()  is after the call.  It is also outside of void loop()  and void setup() .

When writing functions, the declaration must be outside of any other functions including void loop()  and void setup() .  Unlike variable declarations, function declarations do not need to be before the call.



Functions with Inputs

A useful feature of functions is the ability to accept inputs.  Recall the function digitalWrite()

This will set pin 11 to 1, or HIGH.

If I wanted to set pin 14 to 1, I would use

So the function remains the same because it performs basically the same operation.  The only thing that changes is the pin number.


Adding Inputs to the Declaration

In the declaration, you initialize the variables you expect to receive

Here we expect to receive two input integers, x  and y .  These variables are only used within the function.  You cannot use the variables x  and y  outside of this function.


Now that these variables are declared, we can use them normally inside of the function.

If you want three inputs, you would simply add another variable declaration


Adding Inputs to the Call

Now that our declaration is expecting inputs, we have to include the proper number of values with our call.

Going with our previous example

MyFunction is expecting two inputs, so we add two values to the call



our call

sets the first input x  to 5 and second input y  to 9.


This is entirely dependent on the order of the input parameters.  If we had switched the order


the variables x  and y  would be switched, with x  being 9 and y  being 5.



Example 2: Input Duration

Taking our earlier   GoForward()  example

This makes our CoDrone go forward for 2000 milliseconds.  What if we wanted to go forward for 3000 milliseconds?


Instead of writing another function for 3000 milliseconds, we can just set the number of milliseconds to an input variable.

Now, when we call the function GoForward, we include a duration.

This will set the CoDrone to go forward for 3000 milliseconds.


Example 3: LEDs Made Easy

In the previous lesson, you learned how to program your controller’s LEDs.  You also probably noticed how tedious it was to control all the LEDs at once.

We can simplify this operation by creating a function

Now, if we want to set all of our LEDs to HIGH, we can write

or to stagger them on and off


Example 4: Simplifying Maneuver Codes

Another set of commands that you have been using often are the CoDrone maneuver commands

This would set your CoDrone to fly upwards and forwards for 4 seconds.  To simplify this, we can create a function called ‘Maneuver’.

Now, to imitate the command I used earlier I could just write


To use this in an autonomous program


Activity:  Function Fiesta


Create a CoDrone code that uses at least three functions:

  1. A function that controls the Controller LEDs
  2. A function that simplifies the CoDrone maneuvers THROTTLE, YAW, PITCH and ROLL
  3. Your own function that performs a CoDrone maneuver