-1

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
}
Tomislav
  • 131
  • 6

2 Answers2

0
void doub(int& x) {
  x = x * 2;
}

would change the value of y in main. This happens because int x makes a copy of the integer, while int& x passes it by reference (something like a pointer, kinda).

Edit to explain references a bit more: Actually, the closer you can get right now in understanding is saying that when you are passing by reference, then x in int& x will be another name for your y variable in main, or in other words, an alias. On the other hand, int x creates a temporary integer that is a copy of the original.

Tetix
  • 317
  • 2
  • 10
  • Thank you for the reply. What do you mean "pass it by reference"? – Tomislav May 01 '19 at 00:52
  • 2
    @Matthieu Have you learned about references and pointers yet? If not, you have some reading to do. Pull out your favorite [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start studying, every decent C++ book/tutorial covers what references are and how to use them. – Remy Lebeau May 01 '19 at 00:53
  • Pass by value is attaching the Word document to an e-mail. The receiver would edit the copy they get, not the original. Pass by reference would be attaching the link to the Office 365 shared folder to the Word document. Now, the receiver would edit the original document itself. – jxh May 01 '19 at 00:55
  • I will begin there then, thanks @RemyLebeau – Tomislav May 01 '19 at 00:55
  • @Matthieu edited answer to explain a bit. – Tetix May 01 '19 at 00:56
  • 1
    @jxh That might be misleading, since the link to the Office 365 shared folder is exactly a _pointer_ and not a reference. The correct word/explanation is **alias**. – Tetix May 01 '19 at 01:15
  • @Matthieu check this code to help you understand the difference between a pointer and a reference: https://onlinegdb.com/H1Ow7dUi4 If `y` was a pointer, you would expect that `x` does **not** change value (but it does). – Tetix May 01 '19 at 01:18
  • It depends on the experience from the receiver. If the receiver is just clicking on a link to edit the document (either as an attached copy or a linked document), then from the receiver's POV, it is by reference. If the receiver needs to do something different for one or the other, then that is analogous to a dereference. – jxh May 01 '19 at 01:32
0

That will print 7 because it's the no pointer case. The value got copied to the stack on function call and popped off on function return.

To make it change is why we have pointers. To make it less ugly is why we have references.

Joshua
  • 40,822
  • 8
  • 72
  • 132