Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- void call_by_value_swap(int v1, int v2) {
- //std::cout << "Address of v1, v2 is (" << &v1 << ", " << &v2 << ")" << std::endl;
- int temp = v1;
- v1 = v2;
- v2 = temp;
- }
- void call_by_address_swap(int *v1, int *v2) {
- //std::cout << "Address of v1, v2 is (" << v1 << ", " << v2 << ")" << std::endl;
- int temp = *v1;
- *v1 = *v2;
- *v2 = temp;
- }
- void call_by_reference_swap(int &v1, int &v2) {
- //std::cout << "Address of v1, v2 is (" << &v1 << ", " << &v2 << ")" << std::endl;
- int temp = v1;
- v1 = v2;
- v2 = temp;
- }
- int main (int argc, char *argv[]) {
- int x = 1;
- int y = 0;
- std::cout << "Origin x, y is (" << x << ", " << y << ")" << std::endl;
- //std::cout << "Origin address of x, y is (" << &x << ", " << &y << ")" << std::endl;
- std::cout << std::endl;
- call_by_value_swap(x, y);
- std::cout << "call_by_value x, y is (" << x << ", " << y << ")" << std::endl;
- std::cout << std::endl;
- x = 1;
- y = 0;
- call_by_address_swap(&x, &y);
- std::cout << "call_by_address x, y is (" << x << ", " << y << ")" << std::endl;
- std::cout << std::endl;
- x = 1;
- y = 0;
- call_by_reference_swap(x, y);
- std::cout << "call_by_reference x, y is (" << x << ", " << y << ")" << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement