Difference between Call by Value and Call by Reference in Programming
Key Difference: Call by value and call by reference are both methods of passing arguments. In call by value, a copy of actual arguments is passed to respective formal arguments; whereas in call by reference the location or address of the actual arguments is passed to the formal arguments.
Call by Value and Call by Reference are two different functions that are primarily utilized in C programing languages. The C programing languages include C, which has often been called one of the most important programming languages out there. The reason for this is that C forms the basis of so many other programming languages out there, including C++ and C #, as well as Java, JavaScript, Perl, PHP, and Python, among so many others.
Call by value and call by reference are both methods of passing arguments. They can be used to pass value or data to another function. However, the manner in which they pass the arguments differ.
The primary difference between the two is that in call by value, a copy of actual arguments is passed to respective formal arguments; whereas in call by reference the location or address of the actual arguments is passed to the formal arguments. Due to this, any changes made in the formal arguments will also reflect in the actual arguments. Hence, care should be taken when using call by reference as it is very easy to accidently make changes in the formal arguments.
Call by Value is generally used as the default function in most C programming languages, especially such as C++, PHP, Visual Basic .NET, C# and REALbasic. However, most of them support Call by Reference by offering special syntax for call-by-reference parameters. Still, in C all the function arguments are passed by value, as C does not support references as the other languages do. The reason for this is the fact that in C the calling and the called functions do not share any memory. As they each have their own copy, the called function cannot directly alter a variable in the calling function. It can only alter its own personal copy. So, the call by reference would be pointless here.
Comparison between Call by Value and Call by Reference in Programming:
|
Call by Value |
Call by Reference |
Description |
A function to pass data or value to other functions |
A function to pass data or value to other functions |
Languages used |
C based programming languages |
C based programming languages such as C++, Java, but not C itself |
Purpose |
To pass arguments to another function |
To pass arguments to another function |
Arguments |
A copy of actual arguments is passed to respective formal arguments |
Reference the location or address of the actual arguments is passed to the formal arguments |
Changes |
Changes are made in the own personal copy. Changes made inside the function are not reflected on other functions |
Any changes made in the formal arguments will also reflect in the actual arguments. Changes made inside the function are reflected outside the function as well. |
Value modification |
Original value is not modified. |
Original value is modified. |
Memory Location |
Actual and formal arguments will be created in different memory location |
Actual and formal arguments will be created in same memory location |
Safety |
Actual arguments remain safe, they cannot be modified accidentally. |
Actual arguments are not safe. They can be accidentally modified. Hence care is required when handling arguments. |
Default |
Default in most programming languages such as C++, PHP, Visual Basic .NET, C# and REALbasic |
Supported by most programming languages, but not as default. |
Reference: Wikipedia, Tutorials Point (Call by Value and Call by Reference), Java T Point, CS Fundamentals Image Courtesy: sitesbay.com, penjee.com
Add new comment