Lesson 10: Functions


Functions, sometimes called “subprograms”, “subroutines” or “methods”, are pieces of code that perform a certain task. Programmers often create functions if they know they have some blocks of code that are used over and over again.
Functions “encapsulate” many lines of code into a single line of code. “Encapsulate” meaning that the many lines of code are enclosed within the function you write.


What happens when you use a function in your code?

Most programming languages provide many built-in functions, like calculating the square root of a number, for example. In general, we don’t care how the square root function works, only that it does it for us and returns to us the proper answer.

When a function is “called” the program “leaves” the current section of code and begins to execute the first line inside the function. The function “flow of control” is:

  1. The program comes to a line of code containing a “function call”.
  2. The program enters the function (starts at the first line in the function code).
  3. All instructions inside of the function are executed from top to bottom.
  4. The program leaves the function and goes back to where it started from.
  5. Any data computed and RETURNED by the function is used in place of the function in the original line of code.


void loop () {
 if (getDistance() < 30) {
 } else {

// ------- Our user functions are defined here

double getDistance() {
 double distance;
 // ...
 // sonar code here
 // ...
 distance = (340.0/duration/2.0))/1000.0;
 return distance;

void stopRover() {

void goForward(int speed) {
 rightServo.write(90 - speed);
 leftServo(90 + speed);


Why do we write functions?

  1. They allow us to conceive of our program as a bunch of sub-steps. (Each sub-step can be a function. When any program seems too hard, just break the overall program into sub-steps!)
  2. They allow us to reuse code instead of rewriting it.
  3. Functions allow us to keep our variable namespace clean (local variables only “live” as long as the function does). In other words, function_1 can use a variable called i, and function_2 can also use a variable called i and there is no confusion. Each variable i only exists when the computer is executing the given function.
  4. Functions allow us to test small parts of our program in isolation from the rest.


Steps to write a function

  1. Understand the purpose of the function.
  2. Define the data that comes into the function from the caller (in the form of parameters)
  3. Define what data variables are needed inside the function to accomplish its goal.
  4. Decide on the set of steps that the program will use to accomplish this goal. (The Algorithm)

Anatomy of a Function

Anatomy of a Function

All functions will have a data type associated with it. The data type of a function defines what type of data can be returned by that function.

short, int, long, float, double, char, boolean are the built-in data types that we know. There is also a special data type called “void” that we use just for functions. “Void” means that there is no value that is returned by the function…”void” means “empty”.

All functions have a return statement at the end. For the special data type void, there is an implied return at the end of the function.


void setup() {}
void loop() {}

You should be very familiar with the functions above. Notice that they start with “void”. It just means that these functions do not return a value.