9

Possible Duplicates:
pass by reference or pass by value?

I'm having difficulty understanding the Pass-by-Value-Result method. I understand Pass-by-Reference and Pass-by-Value, but I'm not quite clear on Pass-by-Value-Result. How similar is it to Pass-by-Value (assuming it is similar)?

Here is the code

#include <iostream>
#include <string.h>
using namespace std;

void swap(int a, int b)
{

  int temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
  int value = 2;
  int  list[5] = {1, 3, 5, 7, 9};


  swap(value, list[0]);

  cout << value << "   " << list[0] << endl;

  swap(list[0], list[1]);

  cout << list[0] << "   " << list[1] << endl;

  swap(value, list[value]);

  cout << value << "   " << list[value] << endl;

}

The objective is to determine the values of value and list if the Pass-by-Value-Result method is used. (NOT Pass-by-Value).

dougle
  • 95
  • 1
  • 1
  • 6
  • Looking at your code, the calls to `swap()` have no external effect. Their parameters are passed by value. I think that "pass by value result" in this case is badly written version of "What is the result if `swap()` uses pass by value?". I could be tempted to think that "pass by value result" means "function result passed by value" (i.e. not returning a reference or pointer), but that makes no sense here since `swap()` does not return anything. – Mike DeSimone Apr 24 '11 at 05:23
  • @Mike Okay, well sometimes pass-by-value-result is referred to pass-by-copy. if that helps – dougle Apr 24 '11 at 05:30
  • Still not seeing the difference between that and pass-by-value. – Mike DeSimone Apr 24 '11 at 05:33
  • I'm assuming "subprogram" is just another word for "function" here. "... and then copied back at subprogram termination." Wait, what? C++ has a mechanism to do that? The only thing I know that gets explicitly copied back at function termination is the return value, and the optimizer is allowed to get rid of that copy. – Mike DeSimone Apr 24 '11 at 05:46
  • @Willy: Pass-by-value-result is not a thing. There are 3 ways to pass objects in C++. Pass-by-value, pass-by-reference, and pass-by-const-reference. – Benjamin Lindley Apr 24 '11 at 05:48
  • The easy way (for me, at least) to think about it is that everything passed to a function is passed by copy, and those copies are discarded on function exit when the stack frame is reclaimed. If you pass a pointer or reference to something, the *pointer or reference* is copied, and nothing happens to the *pointed to or referenced* item. Then, when you access the item via that pointer or reference, you change the item and not the pointer or reference itself. This creates the *illusion* that, say, pass by reference is different from pass by value. Hope that's not confusing. – Mike DeSimone Apr 24 '11 at 05:48
  • Pass-by-value-result is a style of parameter passing not supported by C, C++, or C#, but sometimes emulated via macros in the first two languages. Whereas pass-by-value has the caller copy parameters into temporary variables which are discarded after a function returns, pass-by-value-result copies those variables after the function returns. This would be a very useful paradigm on some embedded platforms which do not support pointer dereferencing, but I know of no C-ish language that supports it. – supercat Jun 22 '11 at 21:21

2 Answers2

9

If you're passing by value then you're copying the variable over in the method. Which means any changed made to that variable don't happen to the original variable. This means your output would be as follows:

2   1
1   3
2   5

If you were passing by reference, which is passing the address of your variable (instead of making a copy) then your output would be different and would reflect the calculations made in swap(int a, int b). Have you ran this to check the results?

EDIT After doing some research I found a few things. C++ Does not support Pass-by-value-result, however it can be simulated. To do so you create a copy of the variables, pass them by reference to your function, and then set your original values to the temporary values. See code below..

#include <iostream>
#include <string.h>
using namespace std;

void swap(int &a, int &b)
{

  int temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
  int value = 2;
  int  list[5] = {1, 3, 5, 7, 9};


  int temp1 = value;
  int temp2 = list[0]

  swap(temp1, temp2);

  value = temp1;
  list[0] = temp2;

  cout << value << "   " << list[0] << endl;

  temp1 = list[0];
  temp2 = list[1];

  swap(list[0], list[1]);

  list[0] = temp1;
  list[1] = temp2;

  cout << list[0] << "   " << list[1] << endl;

  temp1 = value;
  temp2 = list[value];

  swap(value, list[value]);

  value = temp1;
  list[value] = temp2;
  cout << value << "   " << list[value] << endl;

}

This will give you the results of:

1   2
3   2
2   1

This type of passing is also called Copy-In, Copy-Out. Fortran use to use it. But that is all I found during my searches. Hope this helps.

Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
  • Im sorry this is confusing but yes i have ran these results and got these answers for pass-by-value. But what im talking about is Pass-by-value-result which is different than pass-by-value. – dougle Apr 24 '11 at 05:26
  • 2
    If it's copy-in, copy-out shouldn't you be copying the values to temp variables, making the call to swap, and the reassigning the target values after the swap call? The first call looks right but if I understand correctly you want to pass in `temp1` and `temp2` to swap every time. – Andy Gaskell Apr 14 '16 at 07:43
0

Use references as your parameters instead such as:

void swap(int &a, int &b)
{

  int temp;
    temp = a;
    a = b;
    b = temp;
}

a and b will now hold the actual value.

Neigyl R. Noval
  • 6,018
  • 4
  • 27
  • 45