A function is a group of statements that together perform a task. Every Arduino program has at least two function, which are setup() and loop().
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is such that each function performs a specific task.
A function tells the compiler about a function's name, return type, and parameters. The Arduino library provides numerous built-in functions that your program can call. For example, function digitalWrite() to change the state of pin.
A function is known with various names like a method or a sub-routine or a procedure.
The general form of a C function definition is as follows:
A C function definition consists of a function header and a function body. Here are all the parts of a function.
A function may return a value. The return_type is the data type of the value the function returns. Some examples of data types can be found in the variables section. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
This is the actual name of the function. The function name and the parameter list together constitute the function signature.
A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. For example setup() and loop() function don't have any parameter.
The function body contains a collection of statements that define what the function does. Here is where code is written.
Functions that could be used in projects to turn on and off a LED for a period of time.
A function used to change a pin value to HIGH.
A function used to change pin value to LOW.
Example of a program using both function to turn on and off multiple LEDs. The code is much shorter and easy to read.
It is possible to have functions with the same in Arduino, if the parameters list is different. See the examples below, which are valid even if used with the functions described above.
They are very similar, but to just turn on and off without waiting, it is more natural to write on(13), than to write on(13, 0).