Datasheet

BOE Shield-Bot Navigation • Chapter 4
Robotics with the BOE Shield-Bot 119
functions to your sketch as well as a few different approaches to creating reusable
maneuvers with those functions.
Minimal Function Call
The diagram below shows part of a sketch that contains a function named example added at
the end, below the
loop function. It begins and gets named with the function definition void
example(). The empty parentheses means that it doesn’t need any parameters to do its job,
and
void indicates that it does not return a value (we’ll look at functions that return values
in a later chapter). The curly braces
{} that follow this definition contain the example
function’s block of code.
void setup() {
Serial.begin(9600);
Serial.println("Before example function call.");
delay(1000);
example();
Serial.println("After example function call.");
delay(1000);
}
void loop() {
}
void example()
{
Serial.println("During example function call.");
delay(1000);
}
There is a function call to example in the
setup function, labeled in the diagram above.
That
example() line tells the sketch to go find the function with that name, execute its
code, and come back when done. So, the sketch jumps down to
void example() and
executes the two commands in its curly braces. Then, it returns to the function call and
continues from there. Here is the order of events you will see when you run the sketch:
1. The Serial Monitor displays "Before example function call."
2. After a one second delay, the monitor displays "During example function call." Why?
Because the
example() call sends the code to void example(), which has the line
of code that prints that message, followed by a 1 second delay. Then, the function
returns to the
example call in setup.
3. The Serial Monitor displays "After example function call."
…sends sketch
to function.
Function call…
Function starts here.
When done, sketch returns to next instruction after function call.