Reference Parameters

Hello!

Can someone please tell me what reference parameters are? For example, what's the difference between "void subprogram(int n)" and "void subprogram(int &n)"? And what's the difference between pass-by-value and pass-by-reference?
Read the functions tutorial on this site, it explains it all:
http://www.cplusplus.com/doc/tutorial/functions/

Also, keep in mind that there is no such thing as "pass-by-pointer" - pointers can be passed by value or by reference.
Last edited on
closed account (E3h7X9L8)
when you do pass by value the compiler makes a copy of the passed variable of the subprogram, meaning all operation you do using that variable, are actually done on a copy of the original passed variable, the original value remains unchanged

when you do pass by reference you are passing an alias of the original value to the subprogram, that means all operations you do in the subprogram with the original variable as argument, are done on the actual original value

pass by value is "void subprogram(int n)" and pass by reference is "void subprogram(int &n)", the only difference is using "&" to indicate to the compiler that n is supposed to be a refference for the value passed to the subprogram.


Topic archived. No new replies allowed.