How does this swap function work in C++?

In summary, this code uses a swap function to swap the values of the variables a and b. The function takes the reference of the first variable passed to it and the value of the second variable, which means any changes made to the first variable inside the function will affect the original variable. However, since the second variable is only passed by value, any changes made to it inside the function will not affect the original variable. To properly swap the values of the variables, the method signature should be changed to take the reference of both variables.
  • #1
needOfHelpCMath
72
0
I can't understand how this code was able to swap or get this output:

Code:
	Based off of the following code what is the output of the main function?
void Swap(int& x, int y){				
	int temp = x;				
	x = y;						
	y = temp;					
	return;						
}						
				
int main(){

int a =12;
int b = 3;

Swap( b, a);
cout << a << “ “ << b << endl;return 0;
}

output : 12 12
 
Technology news on Phys.org
  • #2
Well if you look at the swap method signature, there is 'int & x' which means it takes the reference of the variable that's passed to the method, while 'int y' will only take the value of the variable that is passed to the method.

So since b is passes to the x in the swap function, what ever you do to x inside the function will actually affect the variable b.

But since only the value of a is passed to y, even if you changed the value of y nothing will happen to the value of a.

But since this is a swap function and actual requirement is to swap the values of a and b you should change the method signature from

Code:
void swap(int & x, int y)
to
Code:
void swap(int & x, int & y)
 

FAQ: How does this swap function work in C++?

What is swap coding?

Swap coding is a programming technique used to exchange the values of two variables without needing a third temporary variable. It is commonly used to rearrange data or sort lists in computer algorithms.

How does swap coding work?

Swap coding works by using the assignment operator (=) to assign the value of one variable to the other. This essentially swaps the values of the two variables, allowing for data to be rearranged or sorted without needing a third temporary variable.

Why is swap coding useful?

Swap coding is useful because it simplifies the process of rearranging data or sorting lists in computer algorithms. It also saves memory by not requiring a third variable to store temporary values.

What are some common applications of swap coding?

Swap coding is commonly used in sorting algorithms such as bubble sort, selection sort, and insertion sort. It is also used in other applications such as swapping the positions of elements in an array or swapping the values of two variables in a mathematical equation.

Are there any limitations to swap coding?

One limitation of swap coding is that it can only be used to swap the values of two variables at a time. This means that more complex data structures, such as multi-dimensional arrays, may require multiple swap coding operations to rearrange or sort the data.

Similar threads

Replies
19
Views
3K
Replies
1
Views
1K
Replies
22
Views
3K
Replies
23
Views
2K
Replies
13
Views
1K
Replies
5
Views
2K
Replies
6
Views
1K
Back
Top