C++ Program to swap two numbers using Call by Reference
Here you will learn the program code of swap two numbers using Call by Reference in C++ programming language.
1 2 3 4 5 6 7 | Call by Reference void swapByRef(int *i, int *j) { //Statement //Statement } |
Example program to swap two numbers using Call by Reference in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //swap two numbers call by reference in c++ #include <iostream> using namespace std; void swapByRef(int*, int*); void swapByRef(int *i, int *j) { int temp; temp = *i; *i = *j; *j = temp; cout<<"\nNumber After Swapping in swap function \na = "<<*i<<"\nb = "<<*j; } int main() { int a = 10, b = 20; cout<<"\nNumber Before Swapping in main function \na = "<<a<<"\nb = "<<b; swapByRef(&a, &b); cout<<"\nNumber After Swapping in main function \na = "<<a<<"\nb = "<<b; return 0; } |
Output
Number Before Swapping in main function
a = 10
b = 20
Number After Swapping in swap function
a = 20
b = 10
Number After Swapping in main function
a = 20
b = 10
Check out our other C++ programming Examples