Last active
May 8, 2017 21:39
-
-
Save titus-shoats/6d035bb60d46522caafa8c77bb74ba64 to your computer and use it in GitHub Desktop.
C++ Pass By References
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <iostream> | |
using namespace std; | |
void FreezeWindow(); | |
void FreezeWindow() { | |
char response; | |
cin >> response; | |
} | |
void swap(int &x, int &y); | |
int main() { | |
int x = 5, y = 10; | |
cout << "Main. Before swap, x: " << x << " y: " << y << endl; // 5, 10 | |
swap(x, y); | |
cout << "Main. After swap, x: " << x << " y: " << y << endl; // 10, 5 | |
FreezeWindow(); | |
return 0; | |
} | |
void swap(int &rx, int &ry) { | |
int temp; | |
cout << " Swap inside swap function. Before swap rx: " << rx << " ry: " << ry << endl; // 5, 10 | |
temp = rx; // when swap is called we are saying --> temp = &5 | |
rx = ry; // when swap is called we are saying --> rx = &10 | |
ry = temp; // when swap is called we are saying --> ry = &5 | |
cout << "Swap inside swap function. After swap, rx: " << rx << " ry: " << ry << endl; // 10, 5 | |
} | |
// output | |
/**** | |
Main Before swap x:5y:`0 | |
Swao inside swao function rx:5 ry:10 | |
Swap inside function. After swap rx:10 ry:5 | |
Main. After swap, x:10 y5 | |
***/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment