Arduino - Functions
Functions in Arduino, like in most programming languages, are blocks of code that perform a specific task. They provide modularity and reusability by allowing you to define a set of instructions that can be called multiple times from different parts of your sketch. Here's how functions work in Arduino:
Declaring and Defining Functions
Syntax
cppreturnType functionName(parameters) {
// Function body
// Statements
return value; // Optional return statement
}
-
returnType
: Specifies the data type of the value that the function returns, such asvoid
,int
,float
,bool
,char
, etc. Usevoid
if the function does not return any value. -
functionName
: The name of the function. Choose a descriptive name that reflects the task the function performs. -
parameters
: Optional list of parameters (inputs) that the function accepts. Parameters are variables declared in the function's header and act as placeholders for values that will be passed into the function when it is called. -
return value
: Optional statement used to return a value from the function. This is only required if the function has a return type other thanvoid
.
Example
cpp// Function declaration
int add(int a, int b) {
int result = a + b;
return result;
}
void setup() {
Serial.begin(9600);
// Function call
int sum = add(3, 5);
Serial.print("Sum: ");
Serial.println(sum);
}
void loop() {
// Empty loop for demonstration
}
Function Declaration vs Definition
In Arduino sketches, functions are typically defined before they are used. This means you declare the function (provide its signature) at the beginning of your code and then define its behavior (write its body) later in the sketch.
Declaration
cpp// Function declaration (prototype)
int add(int a, int b);
Definition
cpp// Function definition
int add(int a, int b) {
int result = a + b;
return result;
}
Calling Functions
To execute a function and perform its task, you call it by its name followed by parentheses ()
containing any required arguments (values to be passed to the function).
cppint sum = add(3, 5); // Calling the add function
Function with No Return Value (void
Functions)
Functions that do not return a value are declared with void
as their return type. They are typically
used for tasks that perform actions but do not produce a result that needs to be returned.
Example
cppvoid blinkLED(int pin, int duration) {
digitalWrite(pin, HIGH);
delay(duration);
digitalWrite(pin, LOW);
delay(duration);
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
blinkLED(LED_BUILTIN, 500); // Blink built-in LED every 500ms
}
Function Parameters
Parameters are variables declared in the function header and act as placeholders for values passed into the function when it is called. They allow functions to accept input values and operate on them.
Example
cpp// Function declaration
void printMessage(String message);
void setup() {
Serial.begin(9600);
printMessage("Hello, Arduino!"); // Calling the function
}
void loop() {
// Empty loop for demonstration
}
// Function definition
void printMessage(String message) {
Serial.println(message);
}
Using Functions for Modularity
Functions help break down complex tasks into smaller, manageable parts. They improve code readability, promote reusability of code blocks, and simplify debugging by isolating specific functionality.
Conclusion
Functions are essential in Arduino programming for organizing code, improving readability, and enabling code reuse. By defining functions with clear purposes and using them effectively, you can build more structured and maintainable Arduino sketches.