Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- void swap(int x, int y);
- void swap2(int* x, int* y); //call by reference
- int main()
- {
- int i = 2;
- int j = 4;
- swap(i, j);
- std::cout << "i = " << i << " , j = " << j << std::endl;
- swap2(&i, &j);
- std::cout << "i = " << i << " , j = " << j << std::endl;
- return 0;
- }
- void swap(int x, int y) //call by value
- {
- int temp = x;
- x = y;
- y = temp;
- }
- void swap2(int* x, int* y) //call by reference
- {
- int temp = *x; //dereferencing x->value
- *x = *y;
- *y = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement