Write a C++ program that explain the call by value functionality.

Program Code:
#include <iostream>
using namespace std;

// function declaration 

void swap(int,int);

// main() function definition with function call


int main()
{
int a,b;
cout<<" Enter Value of first no is :";
cin>>a;
cout<<endl;
cout<<" Enter Value of second no is :";
cin>>b;
cout<<endl;
cout<<" Before swapping";
cout<<endl;
cout<<" Value of first no is : "<<a;
cout<<endl;
cout<<" Value of second no is : "<<b;
cout<<endl;
swap(a,b); // function call in main() function
cout<<endl;
cout<<" Outside function after swapping :";
cout<<endl;
cout<<" Value of first no is : "<<a;
cout<<endl;
cout<<" Value of second no is : "<<b; 
}

// user defined function definition

void swap(int a,int b)
{
int c;
c=a;
a=b;
b=c;
cout<<" Inside function after swapping :";
cout<<endl;
cout<<" Value of first no is : "<<a;
cout<<endl;
cout<<" Value of second no is : "<<b;
}

The program output is tested on www.jdoodle.com
Output:
Enter Value of first no is :
 Enter Value of second no is :
 Before swapping

 Value of first no is : 9
 Value of second no is : 8
 Inside function after swapping :
 Value of first no is : 8
 Value of second no is : 9
 Outside function after swapping :
 Value of first no is : 9
 Value of second no is : 8


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: