Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass-by-value-Result? [duplicate]

Tags:

c++

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).

like image 748
dougle Avatar asked Apr 24 '11 04:04

dougle


People also ask

Does passing by value make a copy?

By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter.

What is the disadvantage of passing by value?

A disadvantage of pass-by-value is that, if a large data item is being passed, copying that data can take a considerable amount of execution time and memory space. Pass-by-reference improves performance by eliminating the pass-by-value overhead of copying large objects.

What does pass by value-result mean?

Pass by Value-Result: This method uses in/out-mode semantics. It is a combination of Pass-by-Value and Pass-by-result. Just before the control is transferred back to the caller, the value of the formal parameter is transmitted back to the actual parameter. This method is sometimes called as call by value-result.

Does passing by value change the original variable?

The question is whether new memory is used for whatever you pass. You can pass an address based on pass-by-value (new storage for the address within the callee) so that changing this address within the callee won't affect the caller's original variable (old storage) that still keeps the original address.


1 Answers

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.

like image 137
Tyler Ferraro Avatar answered Oct 27 '22 18:10

Tyler Ferraro