user42340: I am trying to learn 'call by reference'. I want to call a function fcn1 from the main loop and pass a reference of the local variable var to the function fcn1. Next step is to call a second function fcn2 from the function fcn1 and pass again the reference of the local loop variable var. I always get this error massage:
invalid conversion from 'int' to 'int*' [-fpermissive]

Here a small example code:
void fcn1(int *variable)
{
  fcn2(&variable);
}

void fcn2(int *variable)
{
  //do something with the variable
}

void setup()
{

}

void loop()
{
  int var = 1000;
  fcn1(&var);
}

I tried to solve that issue by using:
void fcn1(int *variable)
{
  fcn2(*variable);
}

But it's the same error.
By deleting the * respectively & it works fine, but is it still a reference?

user37857: void fcn1(int *variable)
{
  fcn2(*variable);
}

This function takes a pointer to an integer as parameter, and then passes the value of the integer to the second function.
To do it by reference, use an ampersand (&, not to be confused with the address-of operator).
void fcn1(int &variable) {
  // Do something to variable
}

For example:
void increment(int &variable) { // Reference to an integer
  variable++;
}
void setup() {
  Serial.begin(115200);
  int a = 1;
  Serial.println(a); // 1
  increment(a);
  Serial.println(a); // 2
}
void loop() {}


You could achieve the same thing using pointers, but using references is much cleaner and easier to read.
void increment(int *variable) { // Pointer to an integer
  ++*variable; // dereference first, then increment value 
}
void setup() {
  Serial.begin(115200);
  int a = 1;
  Serial.println(a); // 1
  increment(&a); // address of a
  Serial.println(a); // 2
}
void loop() {}

Please note: this question is not specific to Arduino, it is just C++. For questions like this, it is best to google for 'C++ pass by reference' instead of 'Arduino pass by reference'.

42340: <p>I am trying to learn 'call by reference'. I want to call a function <code>fcn1</code> from the main loop and pass a reference of the local variable <code>var</code> to the function <code>fcn1</code>. Next step is to call a second function <code>fcn2</code> from the function <code>fcn1</code> and pass again the reference of the local loop variable <code>var</code>. I always get this error massage:</p>

<pre><code>invalid conversion from 'int' to 'int*' [-fpermissive]
</code></pre>

<p>Here a small example code:</p>

<pre><code>void fcn1(int *variable)
{
  fcn2(&amp;variable);
}

void fcn2(int *variable)
{
  //do something with the variable
}

void setup()
{

}

void loop()
{
  int var = 1000;
  fcn1(&amp;var);
}
</code></pre>

<p>I tried to solve that issue by using:</p>

<pre><code>void fcn1(int *variable)
{
  fcn2(*variable);
}
</code></pre>

<p>But it's the same error.</p>

<p>By deleting the <code>*</code> respectively <code>&amp;</code> it works fine, but is it still a reference?</p>
 37857: <pre><code>void fcn1(int *variable)
{
  fcn2(*variable);
}
</code></pre>

<p>This function takes a pointer to an integer as parameter, and then passes the value of the integer to the second function.</p>

<p>To do it by reference, use an ampersand (<code>&amp;</code>, not to be confused with the address-of operator).</p>

<pre><code>void fcn1(int &amp;variable) {
  // Do something to variable
}
</code></pre>

<p>For example:</p>

<pre><code>void increment(int &amp;variable) { // Reference to an integer
  variable++;
}
void setup() {
  Serial.begin(115200);
  int a = 1;
  Serial.println(a); // 1
  increment(a);
  Serial.println(a); // 2
}
void loop() {}
</code></pre>

<hr>

<p>You could achieve the same thing using pointers, but using references is much cleaner and easier to read.</p>

<pre><code>void increment(int *variable) { // Pointer to an integer
  ++*variable; // dereference first, then increment value 
}
void setup() {
  Serial.begin(115200);
  int a = 1;
  Serial.println(a); // 1
  increment(&amp;a); // address of a
  Serial.println(a); // 2
}
void loop() {}
</code></pre>

<p>Please note: this question is not specific to Arduino, it is just C++. For questions like this, it is best to google for 'C++ pass by reference' instead of 'Arduino pass by reference'.</p>
