Junior — Middle
Kuidas edastada põhityüp viitega funktsioonile C++-is?
sobes.tech AI
Vastus AI-lt
In C++, basic types (e.g., int, double) can be passed to a function by reference using the & operator in the parameter declaration. This allows the function to directly modify the argument's value, without copying.
Example:
void increment(int& value) {
value += 1;
}
int main() {
int a = 5;
increment(a); // a becomes 6
return 0;
}
This way, passing by reference is efficient and allows the function to modify the original data.