Pasing value by reference/returning n values

Hi

I wonder, is there a way to pass a variable's value on Arduino by reference, like in C++ ( say, int function(int &number) { ... } )

Essentially, I want to make a function that returns three different values to three separate variables. Return would work just for single variable. I tried to simply use "&" in arguments declaration list, but compiler wouldn't allow me :wink:

Sure enough I can group my variables in array, then function works on actual variable ( or rather memory address it's assigned to ) and not a copy and right now this is a solution I'm using. But nonetheless I would like to find out if there are alternatives available.

C does not have the concept of "reference" ala c++. Instead you can use a pointer. It's basically the same as a reference (the main differences being pointer arithmetic and initialization).
Here is a code snippet:

#include <stdio.h>

void my_func(int* a, int *b, int *c) {
  int temp = *a;
  *a = *b;
  *b = *c;
  *c = temp;
}


void main(void) {
  int i = 23;
  int j = 45;
  int k = 56;
  printf("i=%d, j=%d, k=%d\n", i, j, k);
  my_func(&i, &j, &k);
  printf("i=%d, j=%d, k=%d\n", i, j, k);
}

a pointer to an integer is declare like this:
int * my_pointer;

Two operators for pointer:

  • to dereference a pointer (the value of what is pointed by your pointer)
    & to get a pointer to a variable

Chelmi

C does not have the concept of "reference" ala c++.

True enough. However, a sketch is compiled as a C++ file and therefore references (as well as other C++ features) can be used.