Click here to Skip to main content
14,603,917 members
Rate this:
Please Sign up or sign in to vote.
See more:
My lecturer gave me the task of using the call by reference idea to find the largest out of two numbers. I have always used call by value in a function and actually this is the first time i learnt using call by reference. So, before showing my lecturer my program wanted to seek some advise if my program is correct. Please any comments(as long as it is not offensive) will be highly appreciated.

double largestfun(double &num1, double &num2)
{
    double *temp;
    double *zamp;

    temp = &num1;
    zamp = &num2;

    //return (max(*temp,*zamp));
    if(*temp>*zamp)
    {
        return (*temp);
    }
    else
        return (*zamp);


}

int main()

{
double a, b;
double result;


cout<<"Enter two numbers"<<endl;
cin>>a>>b;

result = largestfun(a,b);

cout<<"The result is:"<<result"<<endl;


system("pause");

}
Posted
Comments
Mehdi Gholam 11-Oct-14 15:06pm
   
Run the program and check the results, then you can see if it is correct.
Surajit Das 11-Oct-14 15:07pm
   
@mehdi gholam:- I ran the program and it gave me correct results. I want to know if my concept is right or wrong
Rate this:
Please Sign up or sign in to vote.

Solution 1

What do you need the local temp and zamp variable for?

double largestfun(double &num1, double &num2)
{
    //double *temp;
    //double *zamp;

    //temp = &num1;
    //zamp = &num2;

    //return (max(*temp,*zamp));
    if(num1 > num2)
    {
        return (num1);
    }
    else
        return (num2);


}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Your code is correct, but unnecessarily complicated. This is how it normally would be done.
double largestfun (double &num1, double &num2)
{
    if (num1 > num2)
        return num1;
    else
        return num2;
}

Or yet simpler:
double largestfun (double &num1, double &num2)
{
    return num1 > num2 ? num1 : num2;
}

And it has nothing to do with call by-reference. The same would work equally well with call by value.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

If you do not modify the referenced entities in your function, make them const references.
E.g.
double max_double(const double &a, const double & b) { return a < b ? b : a; }

The interesting question was, why/when would you use call by references instead of call by value?
Cheers
Andi

PS: Comments to your code:

  • references should be const if they do not modify the arguments - this is idiomatic C++
  • inconsistent use of block statements: use for both branches (if and else) block statement, i.e. {...}
  • over-complicated: why storing parameters in local variables?
  • using the name "temp" is for me a red flag: if you fail to give decent names you are either lazy or did not understand the problem (you might argue why I use a and b instead of other names: may reasoning is that they are interchangeable arbitrary arguments - I prefer names/single characters over numbering the arguments)
  • Why the heck do you assign references to pointers?! That's technically possible but otherwise plain wrong. The idea of having references is to have alias of an outer entity within the function - so, one directly accesses the references by their name. If you take the address of a reference, the chance is high that you sooner or later keep a handle to a "dead object". Just stop the habit to take the address of a reference. If the logic of the program is such that you need to pass a 0-object, pass the object by pointer that allows to pass a 0-pointer.
   
v3
Comments
Surajit Das 11-Oct-14 19:01pm
   
@Andreas Gieriet:- Thanks for the correction.Why do we use the call by reference when we can use the call by value? Whats the main reason behind it?
Andreas Gieriet 12-Oct-14 2:50am
   
Your lecturer should really tell you that. One thing is the technical capability to apply the references, but the *why* and *when* is definitively part of your course! Read your text book. You can also in addition read the last section of When to pass parameters by value, reference, and pointer.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100