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?

Shri
  • 834
  • 1
  • 12
  • 36
tacqy2
  • 309
  • 1
  • 5
  • 12
  • 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. – dxiv Feb 28 '16 at 06: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. – user4581301 Feb 28 '16 at 06: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`. – anukul Feb 28 '16 at 06:31
  • @AnukulSangwan When the closer is attributed to Community, that means the asker approved the duplicate themselves. – Jeffrey Bosboom Feb 28 '16 at 07: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?` – anukul Feb 28 '16 at 08:06

4 Answers4

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.

anukul
  • 1,922
  • 1
  • 19
  • 37
  • 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`. – user4581301 Feb 28 '16 at 06:34
  • 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 Feb 28 '16 at 06:43
  • @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. – Paul Rooney Feb 28 '16 at 06:51
  • @PaulRooney Ahh, I see. That makes sense. Thanks :) – tacqy2 Feb 28 '16 at 06:54
  • 1
    @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) . – Paul Rooney Feb 28 '16 at 06:55
  • @PaulRooney Yes, that was my confusion!! – tacqy2 Feb 28 '16 at 07:01
  • Fixed. The comments should make it clear now. – anukul Feb 28 '16 at 08:04
  • For the sake of clarity, **passing by value** copies the value to a **different memory address** than the original instead of using *same memory address* as when *passing by reference*. – DRTorresRuiz Jun 19 '22 at 11:20
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.

Community
  • 1
  • 1
Akshay Arora
  • 1,953
  • 1
  • 14
  • 30
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.

Chuck
  • 148
  • 1
  • 6
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).

Sci Prog
  • 2,651
  • 1
  • 10
  • 18