Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C++ Programming » Programs » Functions » Call By Value and Call By Reference Program in C++
Suryateja Pericherla Categories: Functions. No Comments on Call By Value and Call By Reference Program in C++
0
(0)

In this article we will learn how to implement call by value and call by reference program in C++. A program in C++ is provided to implement call by value and call by reference using reference variable. Program is as follows:

#include <iostream>
using namespace std;
void swapval(int x, int y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
}
void swapref(int &x, int &y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
}
int main()
{
	int a, b;
	cout<<"Enter two numbers: ";
	cin>>a>>b;
	cout<<"Before swap a="<<a<<", b="<<b<<endl;
	swapval(a,b);
	cout<<"After swap by value a="<<a<<", b="<<b<<endl;
	swapref(a,b);
	cout<<"After swap by reference a="<<a<<", b="<<b<<endl;
	return 0;
}

 

Input and output for the above program are as follows:

Enter two numbers: 10 20
Before swap a=10, b=20
After swap by value a=10, b=20
After swap by reference a=20, b=10

 

How useful was this post?

Click on a star to rate it!


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory