Introduction
In this lab, we will be discussing how to swap two numbers using functions in C++. There are two methods to solve this problem with the help of functions:
In this lab, we will be discussing how to swap two numbers using functions in C++. There are two methods to solve this problem with the help of functions:
To get started, create a new C++ file in your preferred code editor. You must provide the path and name of the code file at the beginning of the lab. For example: ~/project/main.cpp
.
touch ~/project/main.cpp
In Call by Value, actual parameters are passed while calling the function, and the operations effect on the formal parameters doesn't reflect on the actual parameters. Here's how to implement it:
#include<iostream>
using namespace std;
void swap(int x,int y);
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/
int main()
{
int a,b;
cout<<"Enter the Two Numbers to Swap in C++: ";
cin>>a>>b;
cout<<"\nAfter Swapping of Two Numbers:";
swap(a,b);
return 0;
}
void swap(int x,int y)
{
int z;
/*Extra veriable for storing the value of first or second variable*/
z = x;
/*Copying the first variable value to the tempriory variable*/
x = y;
/*Copying the second variable value to the first variable*/
y = z;
/*Copying the tempriory variable value to the second variable*/
cout<<" "<<x<<" "<<y;
}
To run the code, save the file and open the terminal and type the following command:
g++ main.cpp -o main && ./main
In Call by Reference, we pass the address of the actual parameter in the formal parameter, so the changes on the formal parameters reflect on the actual parameters. Here's how to implement it:
#include<iostream>
using namespace std;
void swap(int *x,int *y);
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/
int main()
{
int a,b;
cout<<"Enter Two Numbers To Swap: ";
cin>>a>>b;
swap(&a,&b);
cout<<"\nAfter Swapping Two Numbers: ";
cout<<a<<" "<<b<<" \n";
return 0;
}
void swap(int *x,int *y)
{
int z;
z = *x;
/*Copying the first variable Address to the temporary variable*/
*x = *y;
/*Copying the second variable Address to the first variable*/
*y = z;
/*Copying the temporary variable Address to the second variable*/
}
To run the code, save the file and open the terminal and type the following command:
g++ main.cpp -o main && ./main
Here is the full code for the main.cpp
file:
#include<iostream>
using namespace std;
void swap(int x,int y);
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/
void swap(int *x,int *y);
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/
int main()
{
int a,b;
cout<<"Enter the first number: ";
cin>>a;
cout<<"Enter the second number: ";
cin>>b;
// Method 1: Call By Value
cout<<"\nSwapping of Two Numbers Using Call By Value\n";
swap(a,b);
// Method 2: Call By Reference
cout<<"\nSwapping of Two Numbers Using Call By Reference\n";
swap(&a,&b);
return 0;
}
void swap(int x,int y)
{
int z;
/*Extra veriable for storing the value of first or second variable*/
z = x;
/*Copying the first variable value to the tempriory variable*/
x = y;
/*Copying the second variable value to the first variable*/
y = z;
/*Copying the tempriory variable value to the second variable*/
cout<<" "<<x<<" "<<y;
}
void swap(int *x,int *y)
{
int z;
z = *x;
/*Copying the first variable Address to the temporary variable*/
*x = *y;
/*Copying the second variable Address to the first variable*/
*y = z;
/*Copying the temporary variable Address to the second variable*/
}
Save the file, open the terminal and type the following command to run the code:
g++ main.cpp -o main && ./main
In this lab, we have learned how to use functions to swap two numbers using the Call by Value and Call by Reference methods in C++ programming language.