943,617 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1262
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 3rd, 2008
0

value and reference

Expand Post »
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 .
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Jan 3rd, 2008
0

Re: value and reference

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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. void foo ( int copy )
  4. {
  5. std::cout<<"In foo the copy is "<< copy <<'\n';
  6. copy = 54321;
  7. std::cout<<"Changing the copy to "<< copy <<'\n';
  8. }
  9.  
  10. int main()
  11. {
  12. int original = 12345;
  13.  
  14. std::cout<<"In main the value is "<< original <<'\n';
  15. foo ( original );
  16. std::cout<<"Back in main the value is "<< original <<'\n';
  17. }
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. void foo ( int& reference )
  4. {
  5. std::cout<<"In foo the reference is "<< reference <<'\n';
  6. reference = 54321;
  7. std::cout<<"Changing the reference to "<< reference <<'\n';
  8. }
  9.  
  10. int main()
  11. {
  12. int original = 12345;
  13.  
  14. std::cout<<"In main the value is "<< original <<'\n';
  15. foo ( original );
  16. std::cout<<"Back in main the value is "<< original <<'\n';
  17. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 3rd, 2008
0

Re: value and reference

C++ Syntax (Toggle Plain Text)
  1. void foo( int val ) {
  2. val = 1;
  3. }
C++ Syntax (Toggle Plain Text)
  1. void foo2( int &ref ) {
  2. ref = 1;
  3. }
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 ...

C++ Syntax (Toggle Plain Text)
  1. // with regard to the above two functions
  2. int main( void ) {
  3. int num = 2;
  4.  
  5. foo( num ); std::cout << num << "\n";
  6. foo2(num ); std::cout<< num << "\n";
  7.  
  8. return 0;
  9. }
The output should be
2
1
Last edited by twomers; Jan 3rd, 2008 at 5:05 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Jan 3rd, 2008
0

Re: value and reference

that is mean in passing by reference the changing in the int main , also affect the function .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Jan 3rd, 2008
0

Re: value and reference

or I think the opposite is right .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Jan 3rd, 2008
0

Re: value and reference

Sorry, I can't parse what you're trying to say.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 3rd, 2008
0

Re: value and reference

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jan 4th, 2008
0

Re: value and reference

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 the main like..
foo(a);

the value stored in memory allocated to integer a 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 to a 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...
Last edited by rajatC; Jan 4th, 2008 at 2:39 am.
Reputation Points: 14
Solved Threads: 7
Junior Poster
rajatC is offline Offline
109 posts
since Dec 2007
Jan 4th, 2008
0

Re: value and reference

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,
C++ Syntax (Toggle Plain Text)
  1. void foo( int val ) {
  2. val = 1;
  3. }
  4. void foo2( int &ref ) {
  5. ref = 1;
  6. }
  7. int main( void ) {
  8. int num = 2;
  9.  
  10. foo( num ); std::cout << num << "\n";
  11. foo2(num ); std::cout<< num << "\n";
  12.  
  13. return 0;
  14. }

is equivalent to this in C:
C++ Syntax (Toggle Plain Text)
  1. void foo( int val ) {
  2. val = 1;
  3. }
  4. void foo2( int *ref ) {
  5. *ref = 1;
  6. }
  7. int main( void ) {
  8. int num = 2;
  9.  
  10. foo( num ); printf("%d\n", num);
  11. foo2(&num ); printf("%d\n", num);
  12.  
  13. return 0;
  14. }

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:
C++ Syntax (Toggle Plain Text)
  1. foo2(5)
because the reference needs to have an addressable expression to point to
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Jan 4th, 2008
0

Re: value and reference

Hi i have done intruduction in c++ and would like to know how many libraries does it have
Reputation Points: 10
Solved Threads: 0
Newbie Poster
imabutha is offline Offline
1 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Urgent C++ Project For Class 11
Next Thread in C++ Forum Timeline: Use of brackets for variable types





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC