|
| 1 | +--- |
| 2 | +date_updated: 2024-06-24 |
| 3 | +layout: tutorials |
| 4 | +title: Pass by Value and Reference with Arduino on ESP32-C3 |
| 5 | +dependancies: |
| 6 | + - name: ESP32 Arduino |
| 7 | + url: https://github.com/espressif/arduino-esp32 |
| 8 | +chips: |
| 9 | + - ESP32-C3-MINI-1-N4 |
| 10 | +dev_board: ESP32-C3-DevKitM-1 |
| 11 | +components: |
| 12 | + - name: ESP32-C3-DevKitM-1 |
| 13 | + url: https://www.aliexpress.com/item/1005003989099547.html |
| 14 | +images: |
| 15 | + prototype: blinky-esp32c3-prototype.jpg |
| 16 | + console: pass-by-value-reference-arduino-esp32c3-console.png |
| 17 | +features: |
| 18 | + - pass |
| 19 | + - value |
| 20 | + - reference |
| 21 | + - pointer |
| 22 | + - esp32c3 |
| 23 | +video: SWIAXcH9MuA |
| 24 | +references: |
| 25 | + - name: Schematic of ESP32-C3-DevKitM-1 |
| 26 | + url: https://dl.espressif.com/dl/schematics/SCH_ESP32-C3-DEVKITM-1_V1_20200915A.pdf |
| 27 | + - name: Pinouts |
| 28 | + url: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html#pin-layout |
| 29 | + - name: Arduino Pins |
| 30 | + url: https://github.com/espressif/arduino-esp32/blob/master/variants/esp32c3/pins_arduino.h |
| 31 | +prerequisites: |
| 32 | + - name: Blinky with Arduino on ESP32-c3 |
| 33 | + url: ./blinky-arduino-esp32c3 |
| 34 | +--- |
| 35 | + |
| 36 | +This simple code the difference between pass by value and pass by reference in C++ using Arduino ESP32-C3. |
| 37 | + |
| 38 | +### Pass by Value |
| 39 | + |
| 40 | +- Syntax Simplicity: The syntax for passing by reference is straightforward and more readable compared to pointers. |
| 41 | +- Automatic Dereferencing: The function parameter is treated like an alias to the original variable, so you do not need to use the dereference operator (*). |
| 42 | +- No Null References: References must be initialized when declared, so you cannot have a null reference, which eliminates some potential runtime errors. |
| 43 | + |
| 44 | +### Pass by Reference |
| 45 | + |
| 46 | +- Syntax Simplicity: The syntax for passing by reference is straightforward and more readable compared to pointers. |
| 47 | +- Automatic Dereferencing: The function parameter is treated like an alias to the original variable, so you do not need to use the dereference operator (*). |
| 48 | +- No Null References: References must be initialized when declared, so you cannot have a null reference, which eliminates some potential runtime errors. |
| 49 | + |
| 50 | +### Pass by reference with pointers |
| 51 | + |
| 52 | +- 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. |
| 53 | +- Null Pointers: Pointers can be null, providing flexibility but also the possibility of null pointer dereferencing errors. |
| 54 | +- C Compatibility: Pointers are compatible with C, making this method necessary when working in C or interfacing with C libraries. |
| 55 | + |
| 56 | +| Aspect | Pass-by-Value | Pass-by-Reference | Pass-by-Reference with Pointers | |
| 57 | +|---------------------------|-------------------------------------|------------------------------------|------------------------------------| |
| 58 | +| **Syntax** | `int value` | `int &value` | `int *value` | |
| 59 | +| **Usage** | Operates on a copy of the variable | Direct access to variable | Use of address-of (`&`) and dereference (`*`) operators | |
| 60 | +| **Modification** | Does not affect original variable | Affects original variable | Affects original variable | |
| 61 | +| **Initialization** | N/A | Must be initialized | Can be null | |
| 62 | +| **Readability** | Most readable, simplest | More readable and less error-prone | Requires explicit pointer handling | |
| 63 | +| **Compatibility** | Works in both C and C++ | C++ only | Works in both C and C++ | |
| 64 | +| **Performance** | Copies data (may be slower for large data) | No copying, efficient | No copying, efficient | |
0 commit comments