+ 3

I need guidance for this Swap function

This is my code. I want to swap the original values of A and B. #include <iostream> using namespace std; void Swap (int A, int B); int main() { int A=1; int B=2; cout<<A; Swap(A,B); cout<<A; return 0; } void Swap (int A, int B) { int cup; cup = A; A = B; B = cup; }

6th Sep 2017, 7:45 PM
Muneeb Zubair Khan
Muneeb Zubair Khan - avatar
3 Respostas
+ 3
You are making copies, use a reference instead by adding & after the data type: void Swap(int& A, int& B){/**/} and don't forget to do the same in the prototype.
6th Sep 2017, 7:52 PM
Dennis
Dennis - avatar
+ 11
use the second, but pass A and B as reference: void Swap( int &A, int &B )
6th Sep 2017, 7:52 PM
Nikolay Nachev
Nikolay Nachev - avatar