Give an example of pass-by-pointer, pass-by-reference, and pass-by-value. (C++) GIVING MEDAL!!!
Passing by a value means to actually send the value to a function. Directly, by putting function(5), in the code is basically the same as doing this indirectly with a variable, or saying x=5 and function(x) Nothing you do in the function will change this value. However, you can only return one thing from a function in C++. What if you need multiple returns or to have it change say an array? This is where pass by reference comes in. You can NOT pass 5 by reference or any other constant. You must pass the address of the variable so you can point at it from inside the function. The call would still be x=5 and function(x), but the function declaration would be different: void function(int& a) { do something with a } See here for more: http://www.cplusplus.com/doc/tutorial/functions2/
Oh, and almost forgot... when you pass by reference, changes inside the function will change the original item. This makes it dangerous because you may not have intended the changes. So it is best to be careful when doing this.
to put it simply , pass by value means that the function receives a copy of the variable`s value and what ever the function does happens to it`s copy not to the original variable, whereas in pass by reference the function has direct access to the variable by virtue of it`s address, so any changes the function does will be reflected on the variable P.S : always be "CAREFUL" while dealing with pass by references
Join our real-time social learning platform and learn together with your friends!