Part 1: The anatomy of a C program

Function

A function is a named block of code that performs a specific task.

​In Arduino programming, functions help organize code into manageable sections. Every function has a name, a return type, and can optionally accept parameters. The structure of a function begins with its definition, which includes what type of data it returns, its name, and any inputs it requires.
When writing functions in Arduino, several conventions help create clear and maintainable code.
  • ​The return type specifies what kind of data the function provides back to the caller, with common options being `void` (no return), `int` (integer), `float` (decimal number), `bool` (true/false), or `String` (text).
  • Parameters define the input a function requires, using types like `int`, `float`, `bool`, or `String` to specify what kind of data the function expects to receive.
  • Function names typically follow camelCase convention and use verb-noun pairs that describe the action performed, such as `calculateAverage()` or `readSensor()`.
  • Parameter names are usually descriptive nouns that indicate their purpose, like `pinNumber`, `durationMs`, or `sensorValue`, making the function's intent clear when reading the code.
Basic Function Demonstration

void setup() {
  Serial.begin(9600);
  displayMessage();  // Calling the function
}

void loop() {
}

void displayMessage() {
  Serial.println("Hello from a function!");
}