#include <iostream> void swap(int &a, int &b) { int temp = a; // Store the value of a in a temporary variable a = b; // Assign the value of b to a b = temp; // Assign the value of temp (original a) to b } int main() { int x = 5; int y = 10; std::cout << "Before swap: x = " << x << ", y = " << y << std::endl; swap(x, y); // Call the swap function std::cout << "After swap: x = " << x << ", y = " << y << std::endl; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter