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

In summary, pass by reference in C++ is a method of passing arguments to a function by directly passing the memory address of the variable instead of its value. This allows for direct access and modification of the original variable, making it different from pass by value. It is commonly used for functions that need to modify the original variable and is implemented by using an ampersand in the function declaration. Pass by reference can be used for all types of variables except for constants and literals.
  • #1
Colton0117
5
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
  • #2
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. :)
 
  • #3
Thanks, the issue was the void it should be:

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

needed the & after the int
 
  • #4
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." :)
 

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

What is pass by reference in C++?

Pass by reference is a method of passing arguments to a function in C++ by directly passing the memory address of the variable instead of its value. This allows the function to directly access and modify the original variable, rather than creating a copy.

How is pass by reference different from pass by value in C++?

In pass by value, a copy of the argument is made and passed to the function, while in pass by reference, the memory address of the original variable is passed. This means that changes made to the argument in pass by reference will also affect the original variable, while changes made in pass by value will only affect the copy.

Why use pass by reference in C++?

Pass by reference is useful for functions that need to modify the original variable, as it allows for more efficient memory usage and avoids the need to return a value. It is also commonly used when working with large data structures, as passing a copy of the structure can be time-consuming and inefficient.

How is pass by reference implemented in C++?

In C++, pass by reference is indicated by using an ampersand (&) before the parameter name in the function declaration. This tells the compiler to pass the memory address of the argument instead of its value. The function can then access and modify the original variable using this memory address.

Can pass by reference be used for all types of variables in C++?

Yes, pass by reference can be used for all types of variables in C++, including primitive data types (int, float, etc.), arrays, and objects. However, it is important to note that pass by reference cannot be used with constant variables or literal values, as they do not have a memory address that can be passed.

Similar threads

Replies
1
Views
1K
Replies
39
Views
4K
Replies
5
Views
2K
Replies
6
Views
1K
Replies
36
Views
4K
Replies
25
Views
2K
Replies
10
Views
2K
Replies
19
Views
3K
Back
Top