Arduino – Functions

Functions are used to perform certain operations. Use Functions if a snippet of code needs to be used multiple times in a program, making the code more transparent and concise. Using the functions will make the code more readable.

The written function can be easily copied and used in another program, making the function portable or reusable. Using these features simplifies debugging and reduces the chance of errors resulting from program changes.

There are two basic functions in an arduino sketch, setup () and loop (). Without these, we cannot create arduino code. Other functions must be declared outside of these two functions.

Syntax of the function:

void myFunction(parameter1, parameter2) 
{
   // Codeblock
}

MyFunction is the name of the function. The parameters in parentheses are the values ​​passed to the function, you can enter several parameters separated by commas. The parameter is optional, may occur that a function only does a certain thing. The code in the code block of the function determines what the function should do.

void myFunction() 
{
   Serial.println("Hello World");
}

The void data type before the function name means that the function has no return value. Of course, the data type must be specified according to the return value, e.g., if the function returns a number, its data type will be int.

int myFunction(int param1, int param2) 
{
  int num3 = param1 + param2;
  return num3;
}

We can declare functions before the setup () and loop () functions.

int myFunction(int param1, int param2)   // the function
{
  int num3 = param1 + param2;
  return num3;
}
void setup()
{

}
void loop()
{

}

We can also write our function after the setup () and loop () sections, but a so-called function prototype must be created above them.

The function prototype consists of the function return value data type, the function name, and the data type of the parameter (s) (if any). The prototype of the function must end with a semicolon (;).

int myFunction(int, int);  // the function prototype

void setup()
{

}
void loop()
{

}

int myFunction(int param1, int param2)   // and the function
{
  int num3 = param1 + param2;
  return num3;
}

Functions are not executed automatically while the program is running unless called. The following example shows a function call.

void myFunction() 
{
   Serial.println("Hello World");
}
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  myFunction();  // function call
}

A function can be called more than once, with other parameters.

int myFunction(int param1, int param2) 
{
  int num3 = param1 + param2;
  return num3;
}
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  int myNum = myFunction(10, 22);
  Serial.println(myNum);

  int myNum = myFunction(20, 32);
  Serial.println(myNum);
}

Ein leistungsstarkes Arduino-Board ist das Arduino Mega2560 mit breadboard shield.

Arduino Mega2560 Rev3 Mikrokontroller
werbung – Arduino Mega2560 Rev3 Mikrokontroller von amazon.de

In the next section, we’ll look at interrupts.