0% found this document useful (0 votes)
25 views

Passing Parameters by References

The document discusses passing parameters by reference in C++. It provides an example of a function that swaps the values of two integer variables by reference without returning anything. The example function definition uses references to swap the integer values passed into it.

Uploaded by

Roderick Bennett
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Passing Parameters by References

The document discusses passing parameters by reference in C++. It provides an example of a function that swaps the values of two integer variables by reference without returning anything. The example function definition uses references to swap the integer values passed into it.

Uploaded by

Roderick Bennett
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PASSING PARAMETERS BY REFERENCES IN C++

http://www.tuto rialspo int.co m/cplusplus/passing _parame te rs_by_re fe re nce s.htm

Co pyrig ht tuto rials po int.co m

We have discussed how we implement c all by referenc e concept using pointers. Here is another example of
call by reference which makes use of C++ reference:
#include <iostream>
using namespace std;
// function declaration
void swap(int& x, int& y);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values.*/
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
// function definition to swap the values.
void swap(int& x, int& y)
{
int temp;
temp = x; /* save the value at address x */
x = y;
/* put y into x */
y = temp; /* put x into y */
return;
}

When the above code is compiled and executed, it produces the following result:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

You might also like