Home Page | Language Reference (extended) | SD Reference | Libraries | Comparison | Changes

Functions

Segmenting code into functions allows a programmer to create modular pieces of code that perform a defined task and then return to the area of code from which the function was "called". The typical case for creating a function is when one needs to perform the same action multiple times in a program.

For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB in BASIC).

Standardizing code fragments into functions has several advantages:

There are two required functions in an Arduino sketch, setup() and loop(). Other functions must be created outside the brackets of those two functions. As an example, we will create a simple function to multiply two numbers.

Example

To "call" our simple multiply function, we pass it parameters of the datatype that it is expecting:

void loop{int i = 2;int j = 3;int k;k = myMultiplyFunction(i, j); // k now contains 6}

Our function needs to be declared outside any other function, so "myMultiplyFunction()" can go either above or below the "loop()" function.

The entire sketch would then look like this:

void setup(){Serial.begin(9600);}void loop{int i = 2;int j = 3;int k;k = myMultiplyFunction(i, j); // k now contains 6Serial.println(k);delay(500);}int myMultiplyFunction(int x, int y){int result;result = x * y;return result;}

Another example

This function will read a sensor five times with analogRead() and calculate the average of five readings. It then scales the data to 8 bits (0-255), and inverts it, returning the inverted result.

int ReadSens_and_Condition(){int i;int sval;for (i = 0; i < 5; i++){sval = sval + analogRead(0);    // sensor on analog pin 0}sval = sval / 5;    // averagesval = sval / 4;    // scale to 8 bits (0 - 255)sval = 255 - sval;  // invert outputreturn sval;}

To call our function we just assign it to a variable.

int sens;sens = ReadSens_and_Condition();

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.