Understanding Pass-By-Value-Result

I'm trying very hard to understand pass-by-value-result. However couldn't get the hang of it. I know it's similar to pass-by-reference. But in what way? How does the results differ in the above case? When does it actually use the "reference" of the values?

NewbieCoder asked Dec 9, 2017 at 7:21 NewbieCoder NewbieCoder 706 1 1 gold badge 10 10 silver badges 33 33 bronze badges "I know it's similar to pass-by-reference." What do you mean by similar? Commented Dec 9, 2017 at 7:23 Aren't they similar except when aliasing occurs? Commented Dec 9, 2017 at 7:25

2 Answers 2

This is because, in pass-by-value-result, any modification done to the variables within the calling function, will not be updated until the function exits.

With the same program, using pass-by-reference,

would be printed, because it references to the same address and updates any modification done immediately.

1 1 1 silver badge answered Dec 11, 2017 at 14:51 NewbieCoder NewbieCoder 706 1 1 gold badge 10 10 silver badges 33 33 bronze badges

Probably in your posted code, you wanted "foo(a[i], a[i]);" instead of "p(a[i], a[i]);".

Anyway, your a[0] and a[1] will stay set at 1, because foo() gets arguments by value, and there is no way for foo() to change its arguments in a manner visible to the outside.

Passing by value and passing by reference are similar only because both execute a "passing". Passing by value involves a duplication of the data: foo() receives a copy of a[0] and a copy of a[1]; and foo() calls them x and y. Then foo() increments both, but x and y are copies only visible to foo(), so nothing happens in reality because x and y are no more used after the increment.

Inside foo() there is an increment of the variable i, which is global. This time instead, the variable gets really modified, and the change is visible to everybody.

Passing by reference is different in the sense that foo() would receive an address (the reference) of a variable, and foo() would be able to read but also write "in" that reference, thus modifying the object living there.

Think about this example. I am the main() and you are the foo(). I have a sheet of paper with something written in it, say the title of a song. I can let you know the name of the song by taking another sheet, copying onto it the name, and giving the new sheet to you. This is passing by value.

Different would be if I said to you: "the sheet is on the table" (the table is the location of the data, the "reference"). You go there, look at the sheet, so you know the title of the song. But you could also modify my sheet that lies on the table. This is passing by reference: I tell you where the data is. Of course, to be precise, the way I say to you where the sheet is, is accomplished by taking another sheet and writing on it "the sheet with the title is on the table", then I give you the new sheet. You, foo(), receive a sheet (passed by value), and you can also destroy or modify it, I will never notice. But before modifying this new sheet which will be useless soon or later, use it to look on the table and don't modify MY sheet with the name of the song on it! :-)