Marlin 3D Printer - alias in fonction entry

Hi,

I have a question concerning syntaxe :

In function :

void function (const float & x) {int i = x;}

What is the meaning of "&" ?
What is the difference with code below ?

void function (float x) {int i = x;}

Thks.

Google "C++ reference operator"

Your example is pretty pointless in either form

Okay, Thanks.

What I have understood is that the "&" isn't a pointer in this case, isn't it ?

I'm going to try translate what I have understood (it could help someone else) :

int a = 0;
int& b = a; // that's mean "a" could be called by "b", the variable has 2 names.
a++;
print(a); // write "1"
print(b); // write "1" also cause "a" is the same as "b", even if we don't manipulate "b".

In my case, we use "const float & x" in order to change the value of the entry wich is not a global variable ?

void main() {int i = 0; fct(i); print(i);}
void fct(int& x){x++;}

This will print "1".

int i = 0;
void main() {fct(); print(i);}
void fct() {i++;}

This will do the same : print "1".

Is it true ?

jaune74:

void function (const float & x) {int i = x;}

What is the difference with code below ?

void function (float x) {int i = x;}

There is no functional difference. If you don't change the value of 'x' it doesn't matter if you pass by value or by reference.

If you change the two cases to these:

void function (float &x) {x = 3.14;}
void function (float x) {x = 3.14;}

The first one assigns a new value to whatever variable was passed to the function. The second one does not because, by default, a float variable is passed 'by value' and the dummy variable 'x' just contains a copy of the value.

You can think of the first one as being like:

void function (float *x) {*x = 3.14;}

Passing a 'reference' (&) instead of a pointer (*) means you don't need to de-reference the pointer (*x) every time you use it.

Ok, that's clear for me now.

A friend told me that the utility of this method is to reduice ressources needed and it's important for arduino and similar systems.

Thanks.

An introductory C++ book I’m reading states that references are needed for certain (more advanced) features of the language. I (being an old-time C coder) have been sticking mostly with pointers.