I am learning about programming in C++ specifically dealing with pointers. In the code below, I don't understand why doub(int x)
does not change the value of y
while both trip(int* x)
and quint(int& x)
do. I understand that the last two functions both take in the address of the object while first takes in the object. Any explanation or links to resources is greatly appreciated thanks!
#include <iostream>
using namespace std;
void doub(int x) {
x = x * 2;
}
void trip(int* x) {
*x = *x * 3;
}
void quint(int& x) {
x = x * 5;
}
int main() {
int y = 7;
doub(y);
cout << y << endl; // prints 7 why does this not print 14?
trip(&y);
cout << y << endl; // prints 21
quint(y);
cout << y << endl; // prints 105
}