CPP Program to Implementation of Call by Address

AIM:

To write a c++ program and to implement the concept of Call by Address

Description:

  • In call by reference, original value is modified because we pass reference (address).
  • Here, address of the value is passed in the function, so actual and formal arguments share the same address space.
  • Hence, value changed inside the function, is reflected inside as well as outside the function.

Note: To understand the call by reference, you must have a basic knowledge of pointers.

Algorithm:

  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 the address of the two variable as arguments
  7. Print the value of two variable after swapping
  8. Stop the program

Program:

#include<iostream>
using namespace std;
void swap(int *,int *);
#include<iostream>
using namespace std;
void swap(int &,int &);
int main()
{
int a,b;
cout<<"Enter Value Of A :: ";
cin>>a;
cout<<"\nEnter Value of B :: ";
cin>>b;
cout<<"\nBefore Swapping, Value of :: \n\tA = "<<a<<"\tB = "<<b<<"\n";
swap(&a,&b);
cout<<"\nOutside Function After Swapping, Value of :: \n\tA = "<<a<<"\tB = "<<b<<"\n";
}
void swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
cout<<"\nInside Function After Swapping, Value of :: \n\tA = "<<*a<<"\tB = "<<*b<<"\n";
}

EXECUTION:

Input: 2 7

Output:

Enter Value Of A :: 2
Enter Value of B :: 7
Before Swapping, Value of :: 
	A = 2	B = 7

Inside Function After Swapping, Value of :: 
	A = 7	B = 2

Outside Function After Swapping, Value of :: 
	A = 7	B = 2

Result:

Thus, the Implementation of Call by Address was executed successfully.

Leave a Comment