Functions

A function is a set of instructions, grouped together to perform some specific task. In Arduino programming, we already have two functions loop () and setup () . These are the unavoidable thing in Arduino code.

Structure of a function

The following code is an example of a function that was created to print a dashed line in the Arduino IDE.

void Connection()  // "void" refers return type and “Connection” refers to function name
{
    Serial.println("----------------");
}

The code above that creates the function is called the function definition.

Function Name

When we create a function, it must be given a name. The naming convention for functions is the same as for variables:

  • The function name can be made up of alphanumeric characters (A to Z; a to z; 0 to 9) and the underscore (_).
  • The function name may not start with a number i.e. the numbers 0 to 9.
  • A function name must not be used that is the same as a language keyword or existing function.

The function name ends with parentheses (). Nothing is passed to the example function above, so the parentheses are empty. Passing values or parameters to functions will be explained later in this tutorial.

Return Type

A function must have a return type. The example function does not return anything, so has a return type of void. Returning a value from a function will be explained in the next part of this course.

Function Body

The function body is made up of statements placed between braces {}. The statements make up the functionality of the function (what the function will do when it is called).When a function is used, it is said to be "called". We will look at how to call a function next.

Calling a Function

void setup() {
  Serial.begin(9600);

  Connection(); // calling the function
  Serial.println("| Program Menu |");
  }

void loop() {
}

void DashedLine()   // declaring the function
{
  Serial.println("----------------");
}

Next : Input/Output Functions