Programming

Up to this point we haven't talked much about how to write code for the Launchpad. We have seen many examples, and learning by example is often the fastest way to get something done. But at some point a more organized approach is needed. We have reached that point.

The programming language we have been using on the Launchpad is C++. There are many good books on C++ programming, and hundreds of free on-line tutorials for the language. This chapter will concentrate on the simple parts of the language that are the most used, and we will skip over whole sections of the language that more advanced and complicated, and that are rarely (if ever) used on small computers like the Launchpad.

A C++ program usually starts with a function called main(). When using Energia, the main() function is actually hidden in the startup code. The hidden main() function calls our by-now familiar setup() routine once, and then calls the loop() function over and over again forever. So if you are reading a tutorial about C++ and are wondering where the main() function is, now you know.

A function is a bit of code that does something when it is called. Every bit of code that does something is inside some function or another. Our familiar setup() and loop() functions are good examples.

The simplest functions don't return values to the code that calls them. Our setup() and loop() functions are two examples of functions that don't return values. We tell the C++ compiler that the function has no return value by putting the keyword void in front of the function name. If the calling code does not pass any information to the function, we also use the keyword void to indicate that, by putting it inside the parentheses after the function name.

This is why our setup() and loop() functions look like this:

void
setup( void )
{
}

void
loop( void )
{
}

.