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

Click For Summary
SUMMARY

The forum discussion centers on the C++ function CoordTransform(), which is intended to transform input parameters xVal and yVal into output parameters xValNew and yValNew. The original implementation failed to pass the output parameters by reference, resulting in the output always being (0, 0). The solution is to modify the function signature to include reference operators (&) for the output parameters, changing it to void CoordTransform(int x, int y, int& xValNew, int& yValNew).

PREREQUISITES
  • Understanding of C++ function parameters and return types
  • Knowledge of variable scope in C++
  • Familiarity with reference variables in C++
  • Basic experience with C++ input/output operations
NEXT STEPS
  • Study C++ reference variables and their usage
  • Learn about function overloading in C++
  • Explore C++ memory management and variable scope
  • Investigate the differences between passing by value and passing by reference
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting function parameter issues in C++.

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
12
Views
3K