Fixed issue with C ++ tutorial reference parameters

I have some doubts about C ++ benchmarks. I study from this site:

http://www.doc.ic.ac.uk/~wjk/c++Intro/RobMillerL3.html

      

First program:

#include<iostream>
using namespace std;

int area(int length, int width);        

int main()
{
    int this_length, this_width;

    cout << "Enter the length: ";              
    cin >> this_length;
    cout << "Enter the width: ";
    cin >> this_width;
    cout << "\n";                               

    cout << "The area of a " << this_length << "x" << this_width;
    cout << " rectangle is " << area(this_length, this_width) << endl;

    return 0;
}

int area(int length, int width)   
{
    int number;
    number = length * width
    return number;
}

      

The author then proposes that "under some circumstances it is legal to require a function to change the value of the actual parameter that it passed." After that, he introduces a new function:

void get_dimensions(int& length, int& width)
{
    cout << "Enter the length: ";
    cin >> length;
    cout << "Enter the width: ";
    cin >> width;
    cout << "\n";
}

      

What is the main advantage of passing values ​​as parameters?

+3


source to share


3 answers


Benefits of transfer by reference:

  • This allows us to change the function of the argument value, which is sometimes useful.
  • Since no copy of the argument is executed, it is fast, even when used with large structures or classes.
  • We can follow the const reference to avoid unintentional changes.
  • We can return multiple values ​​from a function.

Disadvantages of passing by reference:



  • Since a reference to a non-const reference cannot be made for a literal or expression, reference arguments must be normal variables.
  • It is difficult to tell if a parameter passed by reference should be injected, displayed, or both.
  • It is impossible to tell from a function call that an argument can change. The argument passed by value and passed by reference looks the same. We can only indicate whether the argument is passed by value or by reference by looking at the function declaration. This can lead to situations when the programmer does not implement the function, changes the value of the argument.
  • Since references are usually implemented by C ++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values ​​passed by reference is slower than accessing values ​​passed by value.

Sources

+3


source


There is already a good answer (imho worth accepting). However, I would like to give a more basic answer as it seems like you first encountered the link:

This function:

void foo(int x){x +=1;}

      

can do anything with the value of the passed (by value) parameter, but it has no chance of returning anything to the caller, i.e. x+=1

has practically no effect.

On the other hand, this function:



void bar(int& x){x +=1;}

      

not only gets the value, but also works on the actual variable that you pass as a parameter (by reference). So the function is x+=1

also outside the function.

Both functions in action:

int main(){
    int a = 1;
    foo(a);      // foo gets a copy of a and increments its value
                 // a is still 1
    bar(a);      // bar directly increments the value of a
                 // a is now 2
}

      

This is the main difference between passing parameters by reference (bar) versus passing by value (foo). The main advantage of passing by reference is that the parameter value does not need to be copied. (This explanation for some reason by value is usually done with a const reference. Passing a reference to a constant is like passing a value, because the value cannot be changed even though the reference is actually passed.) However, for more details, I refer to Rohits answer ...

+1


source


int &a

is a reference to any parameter passed to this function. You should always think of references as an alias to a variable (it is like a pointer const

).

If your link is not const

, you are allowed to modify and therefore modify the contents of the original variable.

This is useful for many reasons, primarily to improve performance by avoiding making copies when passing a parameter by reference, and also useful if you have a function that expects multiple results to return, for example:

int f (int &a,int &b,int &c,int&d);

int main
{
    int first,second,third,result;
    result = f(first,third,result);
} 

      

All of your variables int

can be changed inside your function.

+1


source







All Articles