Functions

Functions allow you to create modular pieces of code that perform certain tasks and then return you to the area of code it was executed from. Below is an example of a function being called:

displayNumber(value);

When Arduino executes this line, it looks for this function's declaration somewhere in the code, passes the "value" variable put inside the () as an "argument" to the function. Below is an example of what the function declaration could look like:

void displayNumber(int incomingValue){
  printInteger(incomingValue);
  // other code in the function
}  

Important (Prototyping)

In C, any function you create yourself the in the body of your code needs a function prototype at the beginning of your code, before the setup() code block. This is similar to the declaration of a variable, and essentially is just the first line of your function declaration, with a semicolon at the end.

void displayNumber(int incomingValue);

This prepares C to know what kind of function you are calling and what arguments it will pass.

Reference Home