how can function EEPROM.get() work?

I don't understand how this function can work because, you have to pass variable not by reference but by value to it (function make copy of variable), so how on earth this function can change variable f?

link

float f = 0.00f;   //Variable to store data read from EEPROM.
int eeAddress = 0; //EEPROM address to start reading from

EEPROM.get( eeAddress, f );
Serial.println( f, 3 );  //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.

Look at the source code for EEPROM.get(). The second argument IS a reference.

I don't understand how this function can work because, you have to pass variable not by reference but by value to it (function make copy of variable), so how on earth this function can change variable f?

Is it not the function definition rather than the function call that determines whether the value is passed by value or reference ? If so, then looking at

EEPROM.get( eeAddress, f );

how would you know which method was being used ?

RayLivingston:
Look at the source code for EEPROM.get(). The second argument IS a reference.

Oh, so you can do it like that? This is really confusing, I am good only at C not C++. Because something like
void function(someType &var) in C will throw error.

void function(someType &var) in C will throw error

Because that is not valid C. It is valid in C++.

Now I know, but it is useful som something? I find it only confusing.

The library also makes use of templated functions. So, again, only applicable to C++.

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