Skip to main content

Posts

Showing posts with the label Write a function using reference variables as arguments to swap the values of a pair of integers in a program

Write a function using reference variables as arguments to swap the values of a pair of integers in a program

 here's an example program that swaps the values of two integers using a function with reference variables: #include <iostream> using namespace std; // function to swap two integers using reference variables void swap(int& x, int& y) {     int temp = x;     x = y;     y = temp; } int main() {     int num1 = 10, num2 = 20;          // before swapping     cout << "Before swapping: " << endl;     cout << "num1 = " << num1 << ", num2 = " << num2 << endl;          // swap the values of num1 and num2     swap(num1, num2);          // after swapping     cout << "After swapping: " << endl;     cout << "num1 = " << num1 << ", num2 = " << num2 << endl;          return 0; } Output: Before swapping: num1 = 10, ...