How to Pass by Value-Result

I'm trying to understand the pass by value-result. The code I have came up with so far only does by value and by reference, which I understand. The value-result is what has me stumped, and honestly I am unsure how to write the function for it. I thought maybe someone could help me understand it better. Here's my code so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

// Function prototypes.
void swapByValue(int, int, int);
void swapByRef(int&, int&, int&);

// Main function.
int main(void)
{
	// Initialize variables.
	int i = 5;
	int j = 2;
	int k = 4;

	// Call swapByValue function.
	swapByValue(i, j, k);
	cout << i << " " << j << " " << k << endl;

	// Call swapByRef function.
	swapByRef(i, j, k);
	cout << i << " " << j << " " << k << endl;
}

/** Make a copy in memory of the actual
 ** parameter's value that is passed in. */
void swapByValue(int num1, int num2, int num3)
{
	int temp = num1;
	num1 = num2;
	num2 = temp;
}

/** Copy of the actual address is stored. */
void swapByRef(int& num1, int& num2, int& num3)
{
	int temp = num1;
	num1 = num2;
	num2 = temp;
}
pass by value-result
No such thing, as far as I know.
It does exist, it's a copy-in-copy-out mechanism. The definition of it states: "An argument passed by value-result is implemented by copying the argument's value into the parameter at the beginning of the call and then copying the computed result back to the corresponding argument at the end of the call."

Still stumped on the meaning and how to turn it into code.

EDIT: Unless, it's not supported in C++...
Last edited on
1
2
3
4
5
6
//This is input. You copy arguments here
//                  ↓         ↓         ↓
void swapByValue(int num1, int num2, int num3)
//↑
//This is output. You can get some value[s] from here
//But you told that you do not want to return anything. 
An argument passed by value-result is implemented by copying the argument's value into the parameter at the beginning of the call and then copying the computed result back to the corresponding argument at the end of the call.
That simply sounds like passing by reference.
Topic archived. No new replies allowed.