Write C++ program to swap two input values using: a) Call by address function. b) Call by reference function.
#include <iostream>
using namespace std;
void SwapByAddress(int* a, int * b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void SwapByReference(int& a, int& b) {
int tmp = a;
a = b;
b = a;
}
int main() {
int a, b;
cout << "Enter a first integer: ";
cin >> a;
cout << "Enter a second integer: ";
cin >> b;
SwapByAddress(&a, &b);
cout << "Swap by address: " << a << " " << b << endl;
SwapByReference(a, b);
cout << "Swap by reference: " << a << " " << b << endl;
}
Comments
Leave a comment