CALL BY REFERENCE:
CALL BY REFERENCE:
The Changes made in the formal arguments will reflect to actual
parameters(arguments).
/*
Example for call by reference */
#include<stdio.h>#include<conio.h>
void swap(int*,int*);
main()
{
int i,j;
printf("enter the values of i and j :");
scanf("%d %d",&i,&j);
swap(&i,&j);
printf("values of i and j after function call: i=%d j=%d",i,j);
return 0;
getch();
}
void swap(int* p,int *q)
{
*p=*p+*q;
*q=*p-*q;
*p=*p-*q;
printf("values of p and q in function : p=%d q=%d",p,q);
}
Swapping program in c using function
using Temporary variable
#include<stdio.h>
void swap(int *,int *);int main(){
int a,b;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
swap(&a,&b);
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}
void swap(int *a,int *b){ //
a=a^b;int *temp; // b=a^b;
temp = a; // a=a^b;
*a=*b; //or a=(a*b)/(b=a); or a=a+b – (b=a); a=a*b;
*b=*temp; // a^=b^=a^=b;
} ////a=a*b;b=a/b; a=a/b;or
Sample output:
Enter any two integers: 3 6Before swapping: a = 3, b=6
After swapping: a = 6, b=6
Comments
Post a Comment