Learn Arduino Tutorials Open the 3D lab

Beginner · 4 min read

Arduino functions explained — write code once, use it anywhere

setup() and loop() are functions you did not write. Your own functions let you give a name to a piece of code and use it again and again. Your sketches get shorter, easier to read and much easier to fix.

An Arduino UNO wired to an ultrasonic distance sensor and red, yellow and green LEDs, a project whose code uses a distanceCm() function
Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →

The short answer

A function is a named block of code. You write it once, then call it by name whenever you need it:

void blinkOnce() {
  digitalWrite(13, HIGH);
  delay(200);
  digitalWrite(13, LOW);
  delay(200);
}

void loop() {
  blinkOnce();
  blinkOnce();
  blinkOnce();   // three blinks, and the blink code exists only once
}

You have used functions since your first sketch: digitalWrite(), delay() and analogRead() are all functions that someone else wrote. Now you can write your own.

The parts of a function

int addTwo(int a, int b) {
  return a + b;
}
PartHereWhat it means
return typeintwhat the function gives back (void = nothing)
nameaddTwowhat you type to call it
parametersint a, int bthe values you hand in
body{ ... }the code that runs
returnreturn a + b;sends the answer back and ends the function

Calling it: int total = addTwo(3, 4); puts 7 in total. The numbers 3 and 4 are called arguments. Inside the function they become a and b.

void: a function that does something

A void function does a job but gives no answer back. The RGB LED project has a good one:

const int RED = 9, GREEN = 10, BLUE = 11;

void setColor(int r, int g, int b) {
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
}

void loop() {
  setColor(255, 0, 0);     // red
  delay(800);
  setColor(0, 120, 255);   // sky blue
  delay(800);
}

Without setColor() every colour would need three analogWrite lines. With it, one line. If you later move the LED to other pins, you change one place.

The colours in the sketch all go through one setColor() function
The colours in the sketch all go through one setColor() function · open it full screen

Returning a value

A function with a type other than void answers a question. The parking sensor asks the HC-SR04 "how far away is it?":

long distanceCm() {
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH) / 58;   // echo time in µs ÷ 58 = cm
}

void loop() {
  long cm = distanceCm();            // the answer comes back here
  digitalWrite(RED, cm <= 15);
}

Reading the sensor takes five lines. Hiding them in distanceCm() makes loop() read like a sentence: get the distance, light the red LED if it is 15 cm or less. See how the sensor works.

Local and global variables

Where you declare a variable decides who can see it:

int count = 0;              // global: every function can use it

void addOne() {
  int step = 1;             // local: lives only inside addOne()
  count = count + step;
}
  • A global variable lives for the whole sketch. Use it for things that must be remembered between calls, like count.
  • A local variable is created when the function starts and disappears when it ends. Two functions can both have a local i without clashing.

Prefer locals. When a function only uses its parameters and its own locals, you can understand it without reading the rest of the sketch.

Order in the sketch

In plain C++ a function must be written before the line that calls it. The Arduino IDE quietly adds those "prototypes" for you, so you can usually put your functions below loop() and it still works. If you ever see "was not declared in this scope", move the function higher, or add a one-line prototype at the top: long distanceCm();.

Common mistakes

  • Forgetting return. A function declared int must return an int on every path. Otherwise the answer is garbage.
  • Calling without brackets. blinkOnce; does nothing. Write blinkOnce();.
  • Using a parameter name before it exists. a and b exist only inside addTwo.
  • A function that uses delay() for a long time. While it waits, nothing else runs. See millis() vs delay().
  • Changing a parameter and expecting the original to change. Arguments are copies. void bump(int n) { n = n + 1; } leaves the caller's variable unchanged.

Check yourself

  1. What does a void function return?

  2. What happens to a local variable when its function ends?

  3. In int total = addTwo(3, 4); what are 3 and 4?

Questions people ask

Can an Arduino function return more than one value?

Not directly. You can return one value, or you can change global variables, or pass in the address of a variable (a pointer or reference) that the function fills. A simple way: return a struct, or split the job into two functions.

Is setup() a function?

Yes. setup() and loop() are functions the Arduino core calls for you: setup() once at start, loop() over and over. Both are void with no parameters.

How many functions can a sketch have?

As many as fit in the UNO's 32 KB of flash memory. Functions cost very little, so there is no reason to avoid them.

Can a function call itself?

Yes, that is called recursion, but on an UNO with only 2 KB of RAM it can run out of memory quickly. Beginners should use a loop instead.

What's next

Use your new skill to tidy an old sketch: pull the sensor reading into a function in the parking sensor. Then learn how to pick one action from many in switch and case.

Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →
Next: Arduino switch case — pick one action from many choices →

Your progress stays on this device — no account, nothing sent anywhere.

Last updated 2026-11-17

Read next