Description
This simple code the difference between pass by value and pass by reference in C++ using Arduino ESP32-C3.
Pass by Value
- Syntax Simplicity: The syntax for passing by reference is straightforward and more readable compared to pointers.
- Automatic Dereferencing: The function parameter is treated like an alias to the original variable, so you do not need to use the dereference operator (*).
- No Null References: References must be initialized when declared, so you cannot have a null reference, which eliminates some potential runtime errors.
Pass by Reference
- Syntax Simplicity: The syntax for passing by reference is straightforward and more readable compared to pointers.
- Automatic Dereferencing: The function parameter is treated like an alias to the original variable, so you do not need to use the dereference operator (*).
- No Null References: References must be initialized when declared, so you cannot have a null reference, which eliminates some potential runtime errors.
Pass by reference with pointers
- Explicit Address Handling: You explicitly pass the address of the variable using the address-of operator (&) and use the dereference operator (*) to access and modify the value.
- Null Pointers: Pointers can be null, providing flexibility but also the possibility of null pointer dereferencing errors.
- C Compatibility: Pointers are compatible with C, making this method necessary when working in C or interfacing with C libraries.
Aspect |
Pass-by-Value |
Pass-by-Reference |
Pass-by-Reference with Pointers |
Syntax |
int value |
int &value |
int *value |
Usage |
Operates on a copy of the variable |
Direct access to variable |
Use of address-of (& ) and dereference (* ) operators |
Modification |
Does not affect original variable |
Affects original variable |
Affects original variable |
Initialization |
N/A |
Must be initialized |
Can be null |
Readability |
Most readable, simplest |
More readable and less error-prone |
Requires explicit pointer handling |
Compatibility |
Works in both C and C++ |
C++ only |
Works in both C and C++ |
Performance |
Copies data (may be slower for large data) |
No copying, efficient |
No copying, efficient |