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...
#include <iostream>
usingnamespace 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;
}
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.
//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.