#include <iostream>
// x is passed by reference so changes affect the caller
void update(int& x, int delta) {
x += delta;
}
int main() {
int32_t x, delta;
std::cin >> x >> delta;
update(x, delta);
std::cout << x;
return 0;
}
Explanation & Logic Summary:
- Passing x as a reference (int&) allows the function to modify the original variable.
- delta is passed by value because it is not modified.
- No pointer syntax is required, improving safety and readability.
- The updated value of x persists after the function returns.
Firmware Relevance & Real-World Context:
- Reference parameters are widely used in embedded APIs to update state or configuration values.
- Avoids raw pointer usage while maintaining zero-copy efficiency.
- Common in driver interfaces, HAL layers, and middleware code.
- Reinforces understanding of data ownership and side effects in firmware functions.