Page 92 - Modular_V2.0_C++_Flikpbook
P. 92
int temp;
temp = num1;
num1 = num2;
num2 = temp;
cout<< "The values inside the swap function after swapping are: "
<< num1 << " and " << num2 << "\n";
}
Call by Reference
In call by reference technique, reference or memory address of the variables are passed to a function
instead of their values. In this technique, changes made to the parameters inside the function are
reflected on the arguments.
Program 4: To swap two numbers by using call by reference method of function call.
#include<iostream.h>
#include<conio.h>
void swap(int *num1, int *num2);
void main()
{
int a = 10, b = 20;
clrscr();
cout<< "The values before swapping are: " << a << " and " << b <<
"\n";
swap(&a, &b);
cout<< "The values after swapping in main are: " << a << " and " <<
b << "\n";
getch();
}
void swap(int *num1, int *num2)
{ DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0,
int temp; The values before swapping are: 10 and 20
The values inside swap function after
temp = *num1;
swapping are: 20 and 10
*num1 = *num2;
The values after swapping in main are: 20
*num2 = temp; and 10
cout<< "The values inside swap function after swapping are: " <<
*num1 << " and " << *num2 << "\n";
}
In the above program, the references of the parameters are passed to the function by using the
address operator (&) as:
swap(&a, &b);
You need to pass the formal arguments to the function with the help of dereference operator (*) as:
swap(int *num1, int *num2)
90
Touchpad MODULAR (Ver. 2.0)

