Some options: Return a struct. Return an std::array. 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 std::… Answer from PieterP on forum.arduino.cc
🌐
Arduino Forum
forum.arduino.cc › projects › programming
returning two values from a function - Programming - Arduino Forum
I'm breaking up some messy code into separate functions, and can't seem to figure out how to pass two parameters into a function, and get two back. The passing part is easy - I can just call get_angles(somenumber, somenu…
Published   February 14, 2012
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Arduino Function with multiple return values - Programming - Arduino Forum
March 8, 2022 - 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
Discussions

How to call function and retrieve two returns in Arduino or C? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
SOLVED! Need guidance - how to return multiple values from one function.
Hi all, I'm trying to do something like this: // library code uint8_t *MyLibrary::getData (void) { uint8_t values[4]; values[0] = width; // these are... values[1] = height; // ...already defined... values[2] = max_chars; // ...elsewhere in... values[3] = max_lines; // ...this library. return ... More on forum.arduino.cc
🌐 forum.arduino.cc
7
0
February 16, 2016
Returning multiple values from a method
Hello again all. I need help again. I want to return 3 values from a method that I am calling within my code. The 3 values are dynamically changing- as in they are streaming from another arduino. Here is the bit of co… More on forum.arduino.cc
🌐 forum.arduino.cc
11
0
June 28, 2017
Returning more than one value from a function.
Can someone please explain me how to make a function for returning more than one value, as in: · What I have to do to return those two numbers from a function? I think some struct is needed but I don't know how to implement that · I do not know if this is the answer you where looking for ... More on forum.arduino.cc
🌐 forum.arduino.cc
5
0
February 6, 2009
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Return two values from Arduino function - Programming - Arduino Forum
November 22, 2022 - Hi everyone; I'm use this custom function to derive Lat Lon data including two returns. float secondpoints(float CurLat , float CurLon, float Distance, float Bearing) { r_CurLon = radians(CurLon); r_CurLat = radians(CurLat); r_Bearing = radians(Bearing); float DestLat = asin(sin(r_CurLat) * cos(Distance / Eradius) + cos(r_CurLat) * sin(Distance / Eradius) * cos(r_Bearing)); float DestLon = r_CurLon + atan2(sin(r_Bearing) * sin(Distance / Eradius) * cos(r_CurLat), cos(Distance / Eradi...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Returning multiple values from a method - Programming - Arduino Forum
June 28, 2017 - Hello again all. I need help again. I want to return 3 values from a method that I am calling within my code. The 3 values are dynamically changing- as in they are streaming from another arduino. Here is the bit of code where the 3 values are printed to the serial window void showData() { if (newData == true) { Serial.print(ackData[0]); Serial.print(" "); Serial.print(ackData[1]); Serial.print(" "); Serial.println(ackData[2]); new...
🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › software › syntax & programs
Returning more than one value from a function. - Syntax & Programs - Arduino Forum
February 6, 2009 - Hi, Can someone please explain me how to make a function for returning more than one value, as in: printf("Type some number: "); scanf(" %i",&somenumber); printf(Type another number: "); scanf (" %i",&anothernumber)…
Find elsewhere
Top answer
1 of 3
1

It looks to me like you are trying to find the right answer to the wrong question, typical of an XY problem. In the example you provide in your own answer, you call the function pointer right after having set its value. In this case, there is no use to the function pointer, as you could directly call either read_chars() or write_chars().

Obviously, this is just because that example is excessively simplified. In a more realistic use case, you would set the function pointer in one part of the code, and call it in a completely different part. Assuming the functions and the pointer are defined like this:

void (*fun_ptr)();

void read_chars(uint8_t address) {
    printf("address of array is 0x%.2x\n", address);
}

void write_chars(uint8_t row, uint8_t col, const char *str) {
    printf("[%d, %d]: %s\n", row, col, str);
}

One section of the program sets the pointer:

// Here we learn what action should be performed.
if (should_read()) {
    fun_ptr = (void(*)()) read_chars;
} else {  // should write
    fun_ptr = (void(*)()) write_chars;
}

and a different section calls it:

// Here we have to perform the action.
if (fun_ptr == (void(*)()) read_chars) {
    ((void(*)(uint8_t)) fun_ptr)(1);
} else if (fun_ptr == (void(*)()) write_chars) {
    ((void(*)(uint8_t, uint8_t, const char *)) fun_ptr)(
            1, 1, "messaage0");
}

Note that this second section must know what function it is calling in order to pass the correct arguments. Hence the if...then test. Note also that, in order to have valid C++, you must explicitly cast the function pointer before calling it. This gets very clumsy, and it is a lot more convenient to store this information (what function should be called) in an enum rather than a function pointer:

enum {
    CALL_NONE, CALL_READ_CHARS, CALL_WRITE_CHARS
} what_to_call = CALL_NONE;

which would be used like this:

// Here we learn what action should be performed.
if (should_read()) {
    what_to_call = CALL_READ_CHARS;
} else {
    what_to_call = CALL_WRITE_CHARS;
}

// ...

// Here we have to perform the action.
if (what_to_call == CALL_READ_CHARS)
    read_chars(1);
else if (what_to_call == CALL_WRITE_CHARS)
    write_chars(1, 1, "messaage1");

That being said, it is often the case that when it is time to perform the action, the part of the code responsible of doing so does not know what arguments should be passed, and this information is more readily available at the time when when learn what action should be performed. This is a quite common situation, and a standard C idiom in this case is to give the function a void * parameter that it will cast to the appropriate type in order to retrieve the data it needs. For instance:

void (*fun_ptr)(void *);
void *fun_ptr_data;

void read_chars(void * data) {
    uint8_t address = * (uint8_t *) data;
    printf("address of array is 0x%.2x\n", address);
}

struct write_chars_data {
    uint8_t row;
    uint8_t col;
    const char *str;
};

void write_chars(void *data) {
    write_chars_data *d = (write_chars_data *) data;
    printf("[%d, %d]: %s\n", d->row, d->col, d->str);
}

Note that, since write_chars() needs to retrieve multiple parameters from a single pointer, it uses a pointer to a struct.

Now, the part of the code that learns what should be done has the responsibility of providing the data:

// Here we learn what action should be performed.
if (should_read()) {
    fun_ptr = read_chars;
    uint8_t *p = new uint8_t;
    *p = 1;
    fun_ptr_data = (void *) p;
} else {  // should write
    fun_ptr = write_chars;
    write_chars_data *p = new write_chars_data;
    p->row = 1;
    p->col = 1;
    p->str = "messaage2";
    fun_ptr_data = (void *) p;
}

The part of the code that has to perform the action now is very simple:

// Here we have to perform the action.
if (fun_ptr)
    fun_ptr(fun_ptr_data);

Note that this code snippet doesn't free the allocated memory. You will have to figure out who will be responsible for that.

This pattern is quite common in libraries, where the client (the user of the library) wants an action to be performed, but the library is responsible for performing that action at the right time. The client would then provide the library a pointer to a function that accomplishes the action (called a “callback”), together with a generic pointer to the data needed by the callback. The use of a generic void * pointer makes sense, because the library cannot know what kind of data the callback will need.

In your case, since you know what kind of data you may need, you could use a pointer to an union instead of a generic pointer:

union fun_data {
    uint8_t address;  // for read_chars
    struct {          // for write_chars
        uint8_t row;
        uint8_t col;
        const char *str;
    };
};

void (*fun_ptr)(fun_data *);
fun_data *fun_ptr_data;

A more idiomatic C++ style would be to pack together the callback function and its data into a “functor”, i.e. an object that can be called like a function, and can also store the data it needs. You would then use inheritance in order to create specific functor types while calling a generic one.

2 of 3
2

I assume, that the fun_ptr points to the different service functions to do some kind of animation other other long running tasks on your display, thus you want to repeatedly call them in a structured way.

What you are currently doing will not really work. When you call a function, you need to provide its arguments. That's the same for a function pointer. You cannot call a function, that demands parameters, without any parameters. While you can still set a function pointer of type (void (*)(void*) to a function of type (void (*)(uint8_t)), the compiler warns so, because you still cannot call the function without any parameter. If you are trying to do so, you will still get an error of too few arguments to function ....

I see 2 ways to go from here:

  • You could overload your functions, so that a version without any parameters exists. Like this:

      void foo(uint8_t i){Serial.println(i);}
      void foo(){foo(10);}
    

    With 10 being the value, with which the function should be called, when you provide no parameters. You can of course also use variables or function calls here, to actually get that value for the parameter i. Then you can use the pointer to call that function:

      void (* p)(void);
      p = &foo;
      p();
    

    The assignment of the pointer will choose the version of the function without any parameters out of the overloaded pool of this function, since that matches the type of the pointer. Note, that I removed the * from the void parameter definition. I don't understand, what that would mean anyway (pointer to void?). The function pointer p() can now be called without any parameters, since it points to a function without any parameters.

  • You could ditch the function pointer and use a simple switch case statement with a state variable (which holds some identifier for the corresponding function), to call the correct service function. I won't go into detail here, since that is already basically, how an FSM can be programmed in C++, which is covered on this site and on the web numerous times.

If I were you, I probably would go with the first option, though that might also depend on the overall logic, that I currently don't really understand (too few information).

🌐
YouTube
youtube.com › pinout
Arduino returning more than one value from a function
Arduino returning more than one value from a functionText option https://pinout.uno/articles/arduino-returning-more-than-one-value-from-a-function/https://ww...
Published   March 29, 2023
Views   846
🌐
Reddit
reddit.com › r › arduino › comments › 6y2lzs › returning_multiple_variables_with_a_function
Reddit - The heart of the internet
September 4, 2017 - Check out our fantastic Community Wiki for all kinds of beginner tips, guides, and a lot more · An unofficial place for all things Arduino! We all learned this stuff from some kind stranger on the internet. Bring us your Arduino questions or help answer something you might know! 😉
🌐
YouTube
youtube.com › mjlorton
Arduino Tutorial #3 - Functions, return values and variables - YouTube
A tutorial on sketch structure, functions, return values and variables. Sketch / code used in tutorial: http://mjlorton.com/forum/index.php?topic=119.0 Buy y...
Published   October 17, 2012
Views   166K
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Obtaining Multiple Outputs from a function; - Programming - Arduino Forum
April 6, 2021 - Hi I am trying to figure out how I can get multiple outputs from the function I have for creating 8-bit RGB values. I am certain the current method does not work - will I need to create a structure? or global variable? please let me know what is the optimal way,I am pretty new to coding with C //initializations int k; //counter variable int n = 3; //set to 256 //int iter = 3; //RGB columns int Niter = n * n * n; int Rval; int Gval; int Bval; int redP = 7; //pinnumber int blueP = 8; //pinnumber...
🌐
Pinout
pinout.uno › arduino-lessons › arduino-returning-more-than-one-value-from-a-function
Arduino returning more than one value from a function - Pinout
June 16, 2023 - But what if there is a need to return two or more variables from a function? ... There are several ways to solve this problem. Now we will consider them. The simplest option is to use global variables inside a function.
🌐
Stack Exchange
arduino.stackexchange.com › questions › 36087 › arduino-as-slave-to-return-multiple-values
i2c - Arduino as Slave to Return Multiple Values - Arduino Stack Exchange
March 20, 2017 - I'm trying to have the master request for analog values from the slave by sending a request to the slave device for one of the six ADC pin values. ... #include <Wire.h> void setup() { Wire.begin(); Serial.begin(115200); } void loop() { Wire.beginTransmission(0x22); // Start communication with Arduino slave at address 0x22 for(byte i=0;i<4;i++) { Wire.write(i); // Request data from analog pin 'i' delay(5); // Give the slave a moment to sample ADC and respond while(Wire.available()) { byte c = Wire.read(); Serial.println(c); // Print returned value to serial window } } Wire.endTransmission(); // End communication with Arduino slave delay(500); }
🌐
Linux Hint
linuxhint.com › return-function-how-to-use-return-arduino
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Arduino Function with multiple return values - #1000 - Programming - Arduino Forum
March 8, 2022 - 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
🌐
YouTube
youtube.com › paul mcwhorter
LESSON 34: Return a Variable Value from a Function in Arduino - YouTube
In this lesson we learn how to return a local variable from a function in arduino. We begin to see how to work with local variables, and how we can get local...
Published   August 22, 2018
Views   40K
🌐
Processing Forum
forum.processing.org › two › discussion › 11297 › return-two-values-from-a-function
Return two values from a function - Processing 2.x and 3.x Forum
June 13, 2015 - int[] temp = new int[2]; int a=1,b=10; void setup() { } void draw() { function(a, b); println(temp[0]);