pass by value and pass by reference c++ code example
Example: pass by value and pass by reference c++
#include <iostream>
using namespace std;
void by_value(int x) {
x *= 2;
}
void by_reference(int &x) {
x *= 2;
}
int main() {
int a1 = 5, a2 = 5;
cout << "Before: a1 = " << a1 << ", a2 = " << a2 << "\n";
by_value(a1);
by_reference(a2);
cout << "After: a1 = " << a1 << ", a2 = " << a2 << "\n";
return 0;
}