4

I am new to programming and here is a simple question about how passing by reference works. In this program, I am calculating roots of a quadratic equation.

void getCoefficients(double &a, double &b, double &c); void solveQuadratic(double a, double b, double c, double &x1, double &x2); void printRoots(double x1, double x2); void error(string msg); int main() { double a,b,c,x1,x2; getCoefficients(a,b,c); solveQuadratic(a,b,c,x1,x2); printRoots(x1,x2); return 0; } 

So, my question is I seem to be passing values to getCoefficients and solveQuadratic from main program but in the function definitions of getCoefficients and solveQuadratic, I seem to be accepting references as arguments and am confused as to how this works?

5
  • Passing a reference lets the called function modify the variables passed into it (that's unlike passing by value, where the function only receives a copy of the values). In your case, and going by the function names, I would assume that getCoefficients fills-in a, b, c, then solveQuadratic receives those coefficients (by value) and fills-in the roots x1, x2, then finally printRoots prints the x1, x2 values. Commented Feb 28, 2016 at 6:24
  • How it works: syntactic black magic. The compiler sees that the function needs to be called with a reference and generates the appropriate code to make happen without any further user intervention. Commented Feb 28, 2016 at 6:26
  • I don't think this is completely a duplicate of the question linked above. The OP is confused about passing values / references in main. Commented Feb 28, 2016 at 6:31
  • @AnukulSangwan When the closer is attributed to Community, that means the asker approved the duplicate themselves. Commented Feb 28, 2016 at 7:52
  • Sorry, I commented on the basis of what was written in the question. my question is I seem to be passing values to getCoefficients and solveQuadratic from main program but in the function definitions of getCoefficients and solveQuadratic, I seem to be accepting references as arguments and am confused as to how this works? Commented Feb 28, 2016 at 8:06

4 Answers 4

4

When passing a variable by reference, whatever changes you make to it in the function are reflected back in the calling function.

On the other hand, when you pass a variable by value, the changes made to it are local, and hence not reflected in the calling function.

For example,

#include "iostream" using namespace std; void function1(int &x, int y) // x passed by reference { x+=y; } void function2(int x, int y) // x passed by value { x+=y; } int main() { int x=10; function1(x, 10); // x is passed by reference (no & while calling) cout << x << endl; // 20 function2(x, 1000);// x is passed by value cout << x << endl; // 20 } 

Notice that whatever value of y you pass in the call to function2 does not make any difference to the second cout statement.

You do not decide whether to pass values or references in main. The function definition decides that for you. Irrespective of pass by value or pass by reference, the format of calling a function remains the same.

Sign up to request clarification or add additional context in comments.

8 Comments

Suggest rewording the first sentence of the last paragraph: "You do not decide whether to pass values or references in the calling function. The definition of the function being called decides that for you." Generalizes the answer by removing a false dependency on main.
Thanks for a detailed response. I am able to understand that one would like to pass values by reference to have changes reflected in the calling function and why x is 20 both the times. But, I still don't understand why you don't use function1(&x,10) in main function?
@tacqy2 you don't need the & because its not a pointer. Its a reference and doesn't require that the address is passed to the called function.
@PaulRooney Ahh, I see. That makes sense. Thanks :)
@Ankul Sangwan this sentence On the other hand, when you pass a variable by value (not using &), is a little bit confusing. It's not clear when or where you intend the & to be used and maybe the OPs source of confusion. Is it in the function definition (i.e. mark the parameter as a reference) or when calling the function (i.e. as the address of the passed variable) .
|
1

void getCoefficients(double &a, double &b, double &c);

This says, "I take 3 parameters - all of the type double & (reference to a double). Reference is quite a lot confused with pointers, so I recommend you read this up first.

Let's call the a,b,c inside the main as main_method_vars.

When you call getCoefficients, whatever this function does to the passed variables inside it, is reflected on the main_method_vars. Actually, this method works with the main_method_vars.

If instead you have void getCoefficients(double a, double b, double c), this means that whenever you call this method with the main_method_vars, this method will copy the values of a,b,c and work with new copies, instead of working with the original passed copies to it.

Comments

0
void getCoefficients(double &a, double &b, double &c); void solveQuadratic(double a, double b, double c, double &x1, double &x2); 

For example, function getCoefficients, variable a,b,c is passed by reference, so the value for the three variables will be changed in the main function also, if their value changed in the getCoefficients function.

Comments

0

Although not directly an answer, you could look at the topic "variable scope". It would explain why the symbols "a", "b" and "c" may or may not not represent the same thing in the different functions. The concept of local variables is a prerequisite of understanding of "pass by value", "pass by pointer" and "pass by reference".

You can also do this first test: try changing the names of parameters in one of the functions, for example getCoefficients(double &first,double&second,double &third).

You can also do this second test: try calling solveQuadratic(10, 20, 30, x1,x2) and getCoefficients(1,-2,1). The first should work, but not the second.

Finally, you can try this third test: change the value of arguments x1 and x2 in the printRootsfunction. Then check if these changes also occurred in the main function (after the call to printRoot, of course).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.