| | |
value and reference
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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)
#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'; }
C++ Syntax (Toggle Plain Text)
#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'; }
I'm here to prove you wrong.
C++ Syntax (Toggle Plain Text)
void foo( int val ) { val = 1; }
C++ Syntax (Toggle Plain Text)
void foo2( int &ref ) { ref = 1; }
C++ Syntax (Toggle Plain Text)
// 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; }
2
1
Last edited by twomers; Jan 3rd, 2008 at 5:05 pm.
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...
when you call this function from the main like..
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
if you call this function from main like...
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...
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.
Life is about being happy...
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
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,
is equivalent to this in C:
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:
because the reference needs to have an addressable expression to point to
so in the example from above,
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
foo2(5)
--
Index of mp3
Index of mp3
![]() |
Similar Threads
- Dev C++ linker errors, undefined reference (C++)
- Dev-C++, GLUI linker error, undefined reference (C++)
- undefined reference errors when using C++ Sockets Library (C++)
- reference parammeters help (C++)
- reference problems (C)
- passing arrays,reference and value parameters (C)
- Macromedia Coldfusion 5 Language Reference (ColdFusion)
Other Threads in the C++ Forum
- Previous Thread: Need Urgent C++ Project For Class 11
- Next Thread: Use of brackets for variable types
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






