HOWTO · Arduino
How to Return Array From Function in Arduino
Learn why C++ functions cannot return built-in arrays by value and how to share array data safely in an Arduino sketch.
On this page
A C++ function cannot return a built-in array by value. In an Arduino sketch, you can instead pass an existing array to a function and fill it, return a pointer to storage that remains alive, or use shared storage such as a global array. The right choice depends on who owns the storage and how long the values must remain available.
The examples below are complete standard C++ programs so their data flow is easy to follow. In a sketch, put the relevant operation in setup() or another function and use Serial.print() in place of std::cout. They are not verified on a physical Arduino board.
Modify an Existing Array in a Function
The simplest option for a fixed-size result is to let the caller create the array and pass it to a function. In a function parameter, an array is adjusted to a pointer, so the function changes the caller’s original elements. Pass the element count as well; the function cannot infer the array’s capacity from that pointer.
Run this example:
#include <iostream>
void fillValues(int values[], int count) {
for (int i = 0; i < count; ++i) {
values[i] = i + 1;
}
}
int main() {
int readings[5];
fillValues(readings, 5);
for (int value : readings) {
std::cout << value << '\n';
}
}
This changes existing storage; it does not return an array. Here readings is a local automatic array, and its elements have no initial values until fillValues writes them. By contrast, arrays with static storage duration, including global arrays, are zero-initialized when the program starts. Integer zero is a value, not a null pointer.
For a local array, a fixed bound such as int readings[5] is widely supported by Arduino C++ toolchains. Some C++ implementations support variable-length arrays as an extension, but they are not standard C++. Prefer a fixed bound or dynamically allocated storage where the target toolchain and memory budget permit it.
Return a Pointer to a Static Local Array
A function may return a pointer to a static local array because that array continues to exist after the function returns. Its size is fixed, and each call returns a pointer to the same storage. A later call can replace values that an earlier caller expected to keep.
#include <iostream>
int* makeValues() {
static int values[3];
for (int i = 0; i < 3; ++i) {
values[i] = (i + 1) * 10;
}
return values;
}
int main() {
int* result = makeValues();
for (int i = 0; i < 3; ++i) {
std::cout << result[i] << '\n';
}
}
The pointer remains valid for the program’s lifetime, but it does not transfer ownership or provide a fresh array on each call. Avoid this pattern when callers need independent results or when the function may be called again before a previous result is consumed.
Allocate and Return an Array Dynamically
Dynamic allocation can be useful when the result size is chosen at runtime, but it adds ownership and memory-management work. The caller must check allocation before using the pointer and release successful allocations with free(). On memory-limited Arduino boards, repeated allocation and release can fragment the heap; prefer fixed storage when the needed capacity is known.
#include <cstdlib>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
int* makeValues(int count) {
if (count <= 0) return nullptr;
const std::size_t maxCount = std::numeric_limits<std::size_t>::max() / sizeof(int);
if (static_cast<std::uintmax_t>(count) > static_cast<std::uintmax_t>(maxCount)) return nullptr;
const std::size_t bytes = static_cast<std::size_t>(count) * sizeof(int);
int* values = static_cast<int*>(std::malloc(bytes));
if (values == nullptr) return nullptr;
for (int i = 0; i < count; ++i) {
values[i] = i + 1;
}
return values;
}
int main() {
const int count = 3;
int* result = makeValues(count);
if (result == nullptr) {
std::cout << "allocation failed\n";
return 0;
}
for (int i = 0; i < count; ++i) {
std::cout << result[i] << '\n';
}
std::free(result);
}
The caller owns the returned block and must call free() exactly once when finished. Do not access the pointer after releasing it, and do not mix malloc() with delete or new with free(). Before multiplying, makeValues compares the positive count with the largest number of int elements that fit in size_t; it returns nullptr if the byte count would overflow, including when size_t is narrower than int.
Modify a Global Array in a Function
A global array offers fixed storage accessible to functions throughout the sketch. This can be convenient for a small program with one shared result, but it couples the function to global state and makes independent or repeated uses harder to reason about. A global array is zero-initialized before setup() runs.
#include <iostream>
int readings[3];
void fillReadings() {
for (int i = 0; i < 3; ++i) {
readings[i] = 10 + i;
}
}
int main() {
fillReadings();
for (int value : readings) {
std::cout << value << '\n';
}
}
The array stays available for the program’s lifetime. Keep its capacity explicit and ensure every function that writes it respects that capacity.
Modify an Array Through a Reference Parameter
A C++ reference parameter can preserve the array’s size in the parameter type. This makes the required capacity part of the function signature and lets the compiler reject a call with a different-sized array. A template can make the helper work with several fixed sizes.
#include <cstddef>
#include <iostream>
template <std::size_t N>
void fillValues(int (&values)[N]) {
for (std::size_t i = 0; i < N; ++i) {
values[i] = static_cast<int>(i + 1);
}
}
int main() {
int readings[4];
fillValues(readings);
for (int value : readings) {
std::cout << value << '\n';
}
}
This avoids passing the capacity separately and still modifies the caller’s array. The helper accepts built-in arrays with a compile-time size; it does not accept a pointer or a dynamically allocated block as though either carried its length.
Choose an Approach for Your Arduino Sketch
For a typical Arduino sketch, create a fixed-size array where it will be used and pass it to a function along with its length. A reference parameter is useful when the helper should accept only arrays of a particular size or when a template should retain the size automatically. Use a global array when shared program-wide state is genuinely helpful.
A pointer to a static local array is suitable only when one shared, fixed-size result is enough and later calls may safely overwrite it. Dynamic allocation is appropriate only when runtime sizing matters and the sketch can manage allocation failure, ownership, release, and heap limitations. In every approach, a function can return a pointer or modify caller-visible storage, but it cannot return a built-in array by value.