Data types

If we wanted a function to add two numbers together, we would need to tell the compiler about the types of numbers we want to add. They can be integers or they can be floating point numbers (so we can express fractions). Either of those types can come in different sizes, with the larger sizes able to hold bigger numbers.

On the Launchpad, the integers types are


The floating point types are


The char type is 8 bits (it can hold numbers from -127 to 127). The short type and the int type are actually the same on the Launchpad, each holding 16 bits (-32,767 to 32,767). The long type is 32 bits (-2,147,483,647 to 2,147,483,647).

The float type is 32 bits, and the double type is 64 bits. They can handle both huge numbers and very tiny numbers, but they are slow on the Launchpad because the Launchpad has no dedicated hardware for calculating with floating point numbers.

So here is what our function to add two 16 bit integers would look like:

add( int a, int b )
  return a + b;

The first int keyword tells the C++ compiler that the add() function will return a 16 bit integer.

The declarations inside the parentheses tell the compiler that the add() function takes two 16 bit integer arguments, which will be referred to in this function as a and b.

The return statement returns the 16 bit value that results from adding a to b.

The function that calls the add() function might have a statement that looks like this:

int answer = add( 14, 92 );

This declares a new variable called answer, that holds a 16 bit integer. The new variable will now hold the value returned by calling the add function with the numbers 14 and 92. The answer variable can then be used later as input to other functions or calculations. For example, we might want to delay by that number of milliseconds:

delay( answer );

Sometimes we want to give a name to a number. This makes it easier to understand what the number is used for, and it makes it easy to change all of the places where the number is used, all at once. We have seen many examples of this earlier. We use the enum statement to do this:


If you omit the equal signs, the enum statement will assign 0 to the first name, 1 to the second, 2 to the third, and so on.