Skip to content

Commit 045e5c9

Browse files
committed
feat: add new tutorial on pass by value vs reference
1 parent be1f9cb commit 045e5c9

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
BOARD?=esp32:esp32:esp32c3
2+
PORT?=/dev/cu.SLAB_USBtoUART*
3+
BUILD=build
4+
5+
.PHONY: default lint all flash clean
6+
7+
default: lint all flash clean
8+
9+
lint:
10+
cpplint --extensions=ino --filter=-legal/copyright,-runtime/references *.ino
11+
12+
all:
13+
arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./
14+
15+
flash:
16+
arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)
17+
18+
clean:
19+
rm -r build
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Demonstrates the difference between pass by value
2+
// and pass by reference in C++ using Arduino ESP32-C3
3+
4+
void setup() {
5+
Serial.begin(115200);
6+
int originalValue = 5;
7+
8+
passByValue(originalValue);
9+
Serial.print("Pass by Value: ");
10+
Serial.println(originalValue); // Outputs 5
11+
12+
passByReference(originalValue);
13+
Serial.print("Pass by Reference: ");
14+
Serial.println(originalValue); // Outputs 10
15+
16+
passByReferenceWithPointer(&originalValue);
17+
Serial.print("Pass by Reference with Pointer: ");
18+
Serial.println(originalValue); // Outputs 15
19+
}
20+
21+
void loop() {
22+
}
23+
24+
void passByValue(int value) {
25+
value = 10;
26+
}
27+
28+
// Linting error:
29+
// Is this a non-const reference?
30+
// If so, make const or use a pointer: int &value
31+
void passByReference(int &value) {
32+
value = 10;
33+
}
34+
35+
void passByReferenceWithPointer(int *value) {
36+
*value = 15;
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 |
Loading

0 commit comments

Comments
 (0)