954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

value and reference

hello , I want to know what is the difference between passing by value and reference , and if you can put an example with simple out put .

bis student
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

Passing by value is better described as pass by copy. A copy of the value is made and sent to a function. This means that if you change the value in the function, the original remains the same:

#include <iostream>

void foo ( int copy )
{
  std::cout<<"In foo the copy is "<< copy <<'\n';
  copy = 54321;
  std::cout<<"Changing the copy to "<< copy <<'\n';
}

int main()
{
  int original = 12345;

  std::cout<<"In main the value is "<< original <<'\n';
  foo ( original );
  std::cout<<"Back in main the value is "<< original <<'\n';
}

Passing by reference means that instead of a copy of the value, you pass the actual object that contains the value. That way when you change the value, the change is also made in the original object:

#include <iostream>

void foo ( int& reference )
{
  std::cout<<"In foo the reference is "<< reference <<'\n';
  reference = 54321;
  std::cout<<"Changing the reference to "<< reference <<'\n';
}

int main()
{
  int original = 12345;

  std::cout<<"In main the value is "<< original <<'\n';
  foo ( original );
  std::cout<<"Back in main the value is "<< original <<'\n';
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
void foo( int val ) {
  val = 1;
}
void foo2( int &ref ) {
  ref = 1;
}


First is by value, second is by reference. You can change the value of 'ref' in foo2 by setting its value and the value will change outside the scope of the function, but in foo the value only changes within foo's scope. Not outside ...

// with regard to the above two functions
int main( void ) {
  int num = 2;

  foo( num ); std::cout << num << "\n";
  foo2(num ); std::cout<< num << "\n";

  return 0;
}

The output should be
2
1

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

that is mean in passing by reference the changing in the int main , also affect the function .

bis student
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

or I think the opposite is right .

bis student
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

Sorry, I can't parse what you're trying to say.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

He's still not clear which is which.

bis student
At this point, you should copy Narue's code into a file, compile it, and run it. Do it for both programs. That's how you will see what does what.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

pass by value...
it just send the value of the variable(one memory location) to the function...and that value is stored in another variable(another memory location) that is declared in the function...
void foo(int b);
when you call this function from themain like..
foo(a);

the value stored in memory allocated to integera is copied to the memory allocated to the integer b in the function foo...if u make any change in value of b inside the function foo ; it will not affect the value in memory allocated to variable a in the main program.....because the two have physically different memory allocated to them

pass by reference...
this is opposite to pass by value...here you send the actual memory location address and the change is made in this ...
example
void foo(int &c)
if you call this function from main like... foo(a);
the address of the memory location allocated toa will be sent to c declared in function foo... and if you make any change in this memory location inside the function foo the change will be made in the value of a also...because the original memory location has been modified...because c is having the memory address of a ...

i hope you got it now...

rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
 

underneath, passing by reference involves passing the pointer to the thing being referenced; maybe this will give some insights into how it works

so in the example from above,

void foo( int val ) {
  val = 1;
}
void foo2( int &ref ) {
  ref = 1;
}
int main( void ) {
  int num = 2;

  foo( num ); std::cout << num << "\n";
  foo2(num ); std::cout<< num << "\n";

  return 0;
}


is equivalent to this in C:

void foo( int val ) {
  val = 1;
}
void foo2( int *ref ) {
  *ref = 1;
}
int main( void ) {
  int num = 2;

  foo( num ); printf("%d\n", num);
  foo2(&num ); printf("%d\n", num);

  return 0;
}


notice how the reference declaration is equivalent to a pointer declaration, except that every time you use the reference, it is dereferenced automatically without you having to use * explicitly like with a pointer; and whenever you call a method with a reference argument, or assign to a reference, you take the address of the variable, without explicitly using & like with a pointer

also note that in the C++ code above, this won't work:

foo2(5)

because the reference needs to have an addressable expression to point to

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

Hi i have done intruduction in c++ and would like to know how many libraries does it have

imabutha
Newbie Poster
1 post since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Many hundreds if not thousands, probably.

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You