| Aspect | Call By Value | Call By Reference |
|---|---|---|
| Definition | Passes a copy of the actual value to the function. | Passes the address/reference of the actual variable to the function. |
| Effect on Original Variable | Changes made inside the function do not affect the original variable. | Changes made inside the function directly affect the original variable. |
| Data Modification | Not possible (works on a copy). | Possible (works on the original). |
| Memory Usage | Requires more memory (due to value copying). | Requires less memory (works on existing data). |
| Execution Speed | Slightly slower due to copying overhead. | Generally faster, especially with large data structures. |
| Safety | Safer, as the original data remains unchanged. | Riskier, as unintended modifications can occur. |
| Used For | When you want to protect original data from being changed. | When you want to manipulate original data within a function. |
| Example Languages | C, Java (primitives) | C++ (via references), Python (mutable types), Java (object references) |
| Pointer/Reference Usage | Not required. Function works with simple data types. | Required. Uses pointers (C/C++) or references (C++, Java, Python). |
| Function Parameter Behavior | Actual and formal parameters are stored in different memory locations. | Actual and formal parameters share the same memory location. |
| Scope of Changes | Changes are local to the function. | Changes are visible globally (outside the function). |
| Example | int x = 10; foo(x); // x remains 10 | int x = 10; foo(&x); // x can be modified within foo |
L'Oréal Brandstorm 2026
Comments
Add comment