Please tell me - Why we use reference as function arguments?
When we pass a value to a function, function copy the value into a new address in RAM, if we pass large data like struct or hash map, passing values as function arguments is not a good Idea.
Passing reference as function arguments saves memory. The function can directly access the data at the reference (memory address) so no need to copy the whole data.
Passing reference as function arguments is faster than passing values as function arguments.
When we pass a value to a function, function copy the value into a new address in RAM .In pass by reference, a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program.
Pass by reference is usually used to save memory and a tiny bit of time.
It is recommended to pass by value only if input variables are to be used over and over again.
An article on it can be found here
For your kind information, array are always been passed as call by reference by default. So you don’t need to worry about this.
First of all you need to know that what does mean call by reference or call by value.
In C/C++ by default the variable are passed as a call by value. So if you wanna use call by reference then you must use pointer concept. But there is a very good advantage of passing variable as call by reference because in this we pass the address of parameter which saves a lot of time of CPU. Y? Because at every time when a function is initialized it is created in data segment which takes a time to assign value and then store, execute etc.
But there is one disadvantage that the changes in value made after passing a variable as reference will reflect to main() function. So if you don’t want to corrupt or change the value at the main then it would not be beneficial to pass as reference.
If YOu still have any doubt then feel free to ask!
So if you are
Well if we want read only access to our variables, we can pass their constant reference as function parameter rather than using pass by value. This way it will create an alias thus saving time as well as only read access granted.
By the way we can also use pass by value result which will be a better option anyway
Well most of the thing has already been said on this question. I would just like to add 1 thing.
References are an alias compared to pass by value in which values are copied into formal parameters and thus reference are efficient. References are safe as compared to pointers as it points to memory location and thus most of the programming languages like Java,Python usually use Reference mechanism.
But if we want read only access to our variables, we can pass their constant reference as function parameter rather than using pass by value. This way it will create an alias thus saving time as well as only read access granted.
All being said but C++ which is rich in concepts have call/pass by value result mechanism which beats other 4 pass mechanism (pass by name parameter passing, pass by value, pass by reference, pass by pointer)
@rashedcs Thanks mate for accepting my answer. Please upvote my answer too