#include<stdio.h>
#include<conio.h>
void interchange( int *num 1, int num 2)
{
int temp;
temp= *num 1;
*num 1= *num 2;
*num 2= temp;
}
int main()
{
int num 1= 50, num 2=70;
interchange(& num 1, & num 2);
printf(“\n Number 1:%d”,num 1);
printf(“\n Number 2: %d”,num 2);
return(0);
}
Output:
Number 1: 70
Number 2: 50
- While passing parameter using call by address , we are passing the actual address of variable to called function.
- Any update made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.
Advantages of Pass by Reference:
- References allow a function to change the value of the argument, which is sometimes useful.
- Because a copy of the argument is not made, pass by reference is fast, even when used with large structs or classes.
- References can be used to return multiple values from a function .
- References must be initialized, so there’s no worry about null values.
Disadvantages of passing by reference:
However, the drawback of using this technique is that if inadvertent changes are caused to variables in called function then these changes would be reflected in calling function as original values would have been overwritten.
Leave a comment