100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Functions in Arduino Sketches

Learn how to define, call, and return values from functions to organize Arduino code and avoid repetition.

Control Flow & FunctionsBeginner9 min readJul 10, 2026
Analogies

Why Functions Matter

A function is a named block of code you can call whenever you need it, letting you write logic once and reuse it many times. Arduino sketches already contain two functions you must define — setup() and loop() — but you can create your own to break a large sketch into clear, testable pieces. Instead of copying the same ten lines to blink an LED in three places, you write blinkLed() once and call it three times.

🏏

Cricket analogy: A function is a set field placement you can call by name — 'attacking slip cordon' — and deploy in any over, rather than re-explaining each fielder's position ball by ball.

Defining and Calling Functions

A function definition has a return type, a name, a parameter list in parentheses, and a body in braces. void means it returns nothing, while int, float, or bool means it hands back a value with the return statement. Parameters are inputs passed in when you call the function, like blinkLed(9, 500) sending a pin and a duration. You call a function simply by writing its name followed by the arguments in parentheses.

🏏

Cricket analogy: The return type is what the play gives back: a review function returns 'out' or 'not out' (a bool), while a fielding drill returns nothing (void) — it just runs the motion.

cpp
// Reusable blink function with parameters
void blinkLed(int pin, int ms) {
  digitalWrite(pin, HIGH);
  delay(ms);
  digitalWrite(pin, LOW);
  delay(ms);
}

// Function that returns a value
float toCelsius(int rawAnalog) {
  float volts = rawAnalog * (5.0 / 1023.0);
  return (volts - 0.5) * 100.0; // TMP36 formula
}

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  blinkLed(13, 250);
  float tempC = toCelsius(analogRead(A0));
  Serial.println(tempC);
}

You can define your custom functions either above or below setup() and loop() in the sketch. The Arduino IDE automatically generates function prototypes, so unlike plain C++ you usually do not need to declare them before use. Complex or overloaded signatures may still need a manual prototype.

Parameters, Return Values, and Scope

Variables declared inside a function are local: they exist only while the function runs and cannot be seen elsewhere. Variables declared outside all functions are global and shared across the whole sketch. By default parameters are passed by value, meaning the function gets a copy — changing it inside does not affect the caller's variable. To let a function modify the caller's data, pass by reference using an & in the parameter type.

🏏

Cricket analogy: A local variable is a fielder's private call inside one over; a global is the scoreboard everyone reads. Pass by value is telling a teammate the score verbally — he cannot alter the real board.

Overusing global variables makes bugs hard to track because any function can change them at any time. Prefer local variables and parameters, and reach for globals mainly when data must persist across loop() iterations, such as a running counter or a button's previous state.

Function Overloading

C++, and therefore Arduino, allows function overloading: two functions can share the same name if their parameter lists differ in number or type. The compiler picks the right one based on the arguments you pass. This is why Serial.print() works with an int, a float, or a String — they are separate overloaded versions. You can write your own overloads, such as a setColor(int r, int g, int b) alongside setColor(int brightness).

🏏

Cricket analogy: Overloading is like the word 'appeal' meaning different things by context: appeal for LBW versus appeal for a catch. Same name, and the umpire resolves which based on the situation, like the compiler on argument types.

  • A function is a reusable named block that lets you write logic once and call it many times.
  • setup() and loop() are required functions; you can define your own to organize a sketch.
  • A definition has a return type, name, parameters, and a body; void returns nothing.
  • return hands a value back to the caller; parameters pass inputs into the function.
  • Local variables live only inside a function; globals are shared across the whole sketch.
  • Arguments pass by value (a copy) unless you use & to pass by reference.
  • Overloading lets same-named functions differ by parameter list, resolved by the compiler.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ArduinoProgrammingStudyNotes#FunctionsInArduinoSketches#Functions#Arduino#Sketches#Matter#StudyNotes#SkillVeris#ExamPrep