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.
Hi @Sid Kraft ,
Thanks for reaching out.
What you want is possible, but the parameters cannot be declared as const references if you intend to change them inside the function. In C++, const means the function is only allowed to read the values, not assign new ones through those references.
One small detail worth calling out is that the parameters also need an actual type in the declaration. In practice, that would look like const double& A or double& A, not just const &A.
If the goal is to have the function update the caller's variables, declare them as non-const references instead, for example:
void Function(double& A, double& B)
{
A = 2.0;
B = 3.0;
}
Inside the function, you assign to the reference directly with A = 2.0; and B = 3.0;. You would not use &A or &B there, because & is the address-of operator, not something needed to modify a referenced value.
Then when you call:
double C = 0.0;
double D = 0.0;
Function(C, D);
the values of C and D will become 2.0 and 3.0.
One other thing to keep in mind is that if you later assign:
C = 0.0;
D = 0.0;
after the function call, that will overwrite the values the function just set. So the reference part is working correctly, but any assignment afterward will naturally replace those updated values.
So in short, use references when you want the original variables updated, and use const references only when you want to prevent the function from modifying them.
Hope this helps! If my explanation and the information I provided were helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.