Arduino Function with multiple return values

Is there a way to create a function in an arduino code with int or void or a boolean array that will output multiple values? Is there a usful example you have

Some options:

  1. Return a struct.
  2. Return an std::array.
  3. Return an std::pair or std::tuple.

Example:

// 1.
struct IntAndABool {
    int value; // use meaningful names in your code
    bool flag;
};

IntAndABool some_function(int a, int b) {
    return {a + b, a > b};
}

// 2.
#include <array>

std::array<int, 3> some_other_function(int i) {
    return {i+1, i+2, i+3};
}

// 3.
#include <tuple>

std::tuple<int, bool> a_third_function(int a, int b) {
    return {a + b, a > b};
}

// 4.
void yet_another_function(int a, int b, int &value, bool &flag) {
    value = a + b;
    flag = a > b;
}
2 Likes
  1. Return via reference (s)

Accessing struct return from a function - Using Arduino / Programming Questions - Arduino Forum

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.