-4

Sorry if the question is very basic.

PROGRAM 1:

#include <iostream>
using namespace std;

int   max(int &a)
{
   a +=100;
   return a;
}

int main ( int argc, char ** argv)
{

  int x=20;
  int y;
  y = max(x);
  cout <<"x , y value is  "<<x<<"and"<<y<<endl;
 }

OUTPUT:

x, y value is 120and120

PROGRAM 2:

#include <iostream>
using namespace std;

int & max(int &a)
{
   a +=100;
   return a;
}

int main ( int argc, char ** argv)
{

  int x=20;
  int y;
  y = max(x);
  cout <<"x , y value is  "<<x<<"and"<<y<<endl;
 }

OUTPUT:

x, y value is 120and120

The only difference between PROGRAM1 and PROGRAM2 is that the second program returns by reference. What is exactly the difference?

Whoami
  • 13,930
  • 19
  • 84
  • 140

6 Answers6

1

PROGRAM1: copies the referenced variable a when returning that,
PROGRAM2: returns a reference to the referenced variable itself (the same reference, actually?).

There will be no difference in output since the value is copied to variable 'y' either way.

However, The PROGRAM1 performs one more copy operation than PROGRAM2.

Problem happens when you do like:

int&  max(int a) // a is  value variable
{
   a +=100;
   int &a1 = a
   return a1;  // you are returning reference to  local 
}

Here In this version of max() variable a scope is local to max() and if your return reference to a. then stack-allocated i will go away and you are referring to nothing. Wrong!

read for more explanation here

Community
  • 1
  • 1
jt234
  • 662
  • 6
  • 16
0

The two programs do not have a problem. The problem occurs only when you return by reference a local variable for the function. This is because that reference will disappear at the end of the function. But as you are using a it will still be in scope after the function call.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

As I understand it, returning by value creates a new variable, returning by reference returns a reference to the original variable, thus saving memory allocation.

For an integer, the difference is probably academic, as the reference will occupy the same amount of space in memory as the value integer would.

LlamaCloud
  • 160
  • 8
  • When returning by reference the question is "when does the referred returned variable stops existing?" – Ed Heal Mar 01 '13 at 14:35
0

If you returned a value of a local variable, the second one would be incorrect. But the difference between these variants is only that function max in the second one returns original object modified in it. What about the first one - it returns a copy of original value after modifying.

So, if you worked with another large type that was full of fields instead of int, it would be better to use the second one.

anton.vodostoev
  • 300
  • 1
  • 4
  • 12
0

The only difference in your two programs is where the value of x is being copied out of x into another place [and in actual code, it probably doesn't really make any difference at all, because the compiler optimizes away the "not necessary" copy].

In your first function, a is an alias for x in main, and it gets copied when return a; into the return value for the function [in x86, that would be the eax register, for all variants of compilers I know how they work]. So we return a copy of the value in x.

In the second example, the same aliasing of x applies, but we return the "alias", in effect, the code could be taken to read:

   int y;
   max(x);
   y = x;

It really makes no difference for the purposes of this code, but if you were to have soemthing like this:

int &max(int &x, int &x)
{
    if (x > y) return x;
    return y;
}


int main()
{
   int a = 10;
   int b = 13;

   int& c = max(a, b);

   c = 12;

   cout << "a=" << a << " b=" << b << " c=" << c;
}

Now we should see b being given the value of 12, since c becomes an alias of b.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

As far as I can tell, both programs are exactly alike. Did you not copy in the correct code? In general, when you pass a variable by reference, you are making changes to the original variable. When you pass it by value, you are passing a copy of the variable. This means, if you pass a variable by reference and make a change to it, the original variable is also changed. This is not so when passing by value.

jason
  • 3,821
  • 10
  • 63
  • 120