Call by Address, variable swapping. function
Write a c++ program and to implement the concept of Call by Address.

Algorithm to solve the above problem:
1. Start the program
2. Include suitable header file
3. Declare a function swap with two pointers variables arguments
4. Declare and initialize the value as two variable in main()
5. Print the value of two variable before swapping
6. Call the swap function by passing address of the two variable as arguments
7. Print the value of two variable after swapping
8. Stop the program

Implementation of the above problem in C++:
#include<iostream>
using namespace std;
void swap(int *x, int *y);
int main()
{
int i, j;
i=25;
j=50;
cout<<"\n Value of i before swapping is:"<<i;
cout<<"\n Value of j before swapping is:"<<j;
swap (&i,&j);
cout<<"\n Value of i after swapping is:"<<i;
cout<<"\n Value of j after swapping is:"<<j;
return 0;
}
void swap(int *x, int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}

The output of the above program is tested on www.jdoodle.com
Output:
 Value of i before swapping is :25
 Value of j before swapping is :50
 Value of i after swapping is :50
 Value of j after swapping is :25


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: