A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
const is what prevents the assignments from working.
In this declaration:
void Function(const float &A, const float &B)
A and B are references, but they are references to const. That means inside Function they are read‑only; any attempt to assign to them:
A = 2.0f; // error
B = 3.0f; // error
violates const‑correctness, so the compiler rejects it. This is exactly what “the variables must be changeable” in the error message refers to.
To modify the caller’s variables via references, the parameters must be non‑const references:
void Function(float &A, float &B) // non-const references
{
A = 2.0f;
B = 3.0f;
}
int main()
{
float C = 0.0f;
float D = 0.0f;
Function(C, D); // C and D are passed by reference
// Now C == 2.0f and D == 3.0f
}
Key points from the language rules:
- Function arguments are passed by value by default; to let the function change the caller’s variables, use reference parameters (e.g.,
float &A). - A reference behaves syntactically like the object it refers to, but if it is a reference to
const, it cannot be assigned to. - Returning multiple values is usually done either by reference parameters, by returning a struct/class, or by returning a
std::tuple/std::pair.
For example, using a struct instead of out‑parameters:
struct Values
{
float a;
float b;
};
Values Function()
{
return {2.0f, 3.0f};
}
int main()
{
Values v = Function();
// v.a == 2.0f, v.b == 3.0f
}
References: