Swap Two Numbers using Function in C++ Last Updated : 02 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Swapping numbers means exchanging the values of the two numbers with each other. For Example, Before Swapping:a = 10, b = 22;After swapping:a = 22, b = 10In this article, we will write a program to swap two numbers using a function in C++. How to Swap Two Numbers Using Function in C++?We will pass the address or reference of the two variables that we want to swap to the function. If we pass them as value, we won't be able to change their value as in pass by value method, no changes are reflected in the original variables. After that, we can use the temporary variable to swap the value of the given number of variables. C++ Program for Swapping Two Numbers Using Function C++ // C++ program to illustrate how to swap two variables using // a function in C++ #include <iostream> using namespace std; // function to swap two variables void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } // driver code int main() { int a = 10; int b = 22; cout << "Before Swapping: " << endl; cout << " a: " << a << " b: " << b << endl; // calling swap swap(&a, &b); cout << "After Swapping: " << endl; cout << " a: " << a << " b: " << b << endl; return 0; } OutputBefore Swapping: a: 10 b: 22 After Swapping: a: 22 b: 10 We can also make use of C++ templates to create a function that can swap the values of any kind. C++ Program for Swapping Using Function Template C++ // C++ program to illustrate how to swap two numbers using // function template #include <iostream> #include <string> using namespace std; // function template to swap two values template <typename T> void swap_func(T& a, T& b) { T temp = a; a = b; b = temp; } // driver code int main() { string s1 = "Geeks", s2 = "for"; int num1 = 10, num2 = 22; cout << "Before Swapping:" << endl; cout << s1 << " " << s2 << endl; cout << num1 << " " << num2 << endl; swap_func(s1, s2); swap_func(num1, num2); cout << "\nAfter Swapping:" << endl; cout << s1 << " " << s2 << endl; cout << num1 << " " << num2 << endl; return 0; } OutputBefore Swapping: Geeks for 10 22 After Swapping: for Geeks 22 10 Comment More infoAdvertise with us Next Article Swap Two Numbers using Function in C++ A abhishekcpp Follow Improve Article Tags : C++ Programs C++ cpp-template Practice Tags : CPP Similar Reads How to Swap Two Numbers Using Pointers in C++? Swapping the values of two numbers is a very common operation in programming, which is often used to understand the basics of variables, pointers, and function calls. In this article, we will learn how to swap two numbers using pointers in C++. Example Input:int x=10;int y=20;Output:int x=20;int y=1 3 min read valarray swap() function in c++ The swap() function is defined in valarray header file. This function is used to swap the content of one valarray with another valarray. Syntax: void swap( valarray& valarray2 ); Parameter: This method accepts a parameter valarray2 which represents the another valarray with which we have to swap 2 min read Swap Two Numbers Without Third Variable in C++ In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two 2 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read Like