Why Isn't My C++ Function Passing By Reference Correctly?

Click For Summary

Discussion Overview

The discussion revolves around a C++ function, CoordTransform(), intended to transform two input parameters into two output parameters by reference. Participants explore issues related to variable scope and the correct use of reference parameters in the function definition.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their implementation of the function and notes that the output does not match expectations, indicating a potential misunderstanding of how to pass parameters by reference.
  • Another participant suggests that the issue may stem from variable scope, proposing that the new values should either be global or returned explicitly.
  • A later reply identifies that the function definition should include reference parameters (using "&") to avoid making copies of the variables, which would allow the function to modify the original variables directly.
  • One participant reiterates the importance of using "&" to indicate that the same variable should be used, not a copy.

Areas of Agreement / Disagreement

Participants generally agree that the issue lies in the way parameters are passed to the function, specifically regarding the use of reference parameters. However, there is no consensus on the best approach to resolve the issue, as some suggest making variables global while others advocate for using references.

Contextual Notes

The discussion does not resolve the broader implications of using global variables versus reference parameters, nor does it clarify the potential consequences of each approach on program design.

Colton0117
Messages
5
Reaction score
0
Define a function CoordTransform() that transforms its first two input parameters xVal and yVal into two output parameters xValNew and yValNew. The function returns void. The transformation is new = (old + 1) * 2. Ex: If xVal = 3 and yVal = 4, then xValNew is 8 and yValNew is 10.

My code:

Code:
#include <iostream>
using namespace std;

void CoordTransform(int xVal , int yVal , int xValNew, int yValNew){ 
    
   xValNew = (xVal +1) *2;
   yValNew = (yVal +1) *2;  
   return;
}

int main() {
   int xValNew = 0;
   int yValNew = 0;

   CoordTransform(3, 4, xValNew, yValNew);
   cout << "(3, 4) becomes " << "(" << xValNew << ", " << yValNew << ")" << endl;

   return 0;
}

My output is (3, 4) becomes (0, 0) when it should be (8, 10), what am I missing or overlooking to operate the void and have x or y ValNew hold its value?
 
Technology news on Phys.org
It looks like an issue of variable scope. The values you are passing to your function are then local to the function. You may need to either make the variables containing the new values global, or return them explicitly. :)
 
Thanks, the issue was the void it should be:

void CoordTransform(int x ,int y ,int& xValNew,int& yValNew)

needed the & after the int
 
Colton0117 said:
Thanks, the issue was the void it should be:

void CoordTransform(int x ,int y ,int& xValNew,int& yValNew)

needed the & after the int

Yes, basically the "&" says "don't make a copy of this variable, use the same variable." :)
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
20
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 19 ·
Replies
19
Views
6K