Arduino: Define your own functions - how it works
To program an Arduino, object-oriented programming is used. Accordingly, you can also define functions (or "methods") yourself. You can find out how to do this in this practical tip.
Arduino: Define methods without a return value
The methods that return no value are well known.
- They always start with the word "void" and the name of the method. Define a new method for the test with the command »void test () {}«.
- Now, in the curly brackets, write the commands that the method is to execute.
Define functions with return value on the Arduino
Alternatively, you can also define a method that returns a return value.
- First write the type of the return value and then the name of the method. An example of this is "boolean ist_trahr () {}".
- Write in the curly brackets »return« and after it, which value should be returned: »boolean ist_trahr () {return true; } "
Arduino: create methods with parameters
Functions with a return value become really practical if these parameters are used.
- An example of this is the method »int add (int a, int b) {return a + b; } ". As you can see, write the type and several temporary variables in the parentheses.
- The function call »add (2, 3);« would now return 5.
In the next practical tip, you will learn how to control a servo motor with a potentiometer.