Pages

Saturday, August 1, 2020

CALL BY REFERENCE



In the 'Call by reference' method, we pass the address of actual parameters to the function.
Therefore any changes made to function's parameter is also effect the actual parameter's data.

#include<stdio.h>
#include<conio.h>
void swap (int *a, int *b)
{
int *c;
*c = *a;
*a = *b;
*b = *c;
}

void main()
{
clrscr();
int a,b;
printf("Enter value for A:");
scanf("%d", &a);
printf("Enter value for B:");
scanf("%d", &b);

printf("\nValues before call the swap function");
printf("\nValue of A: %d", a);
printf("\nValue of B: %d", b);

// Calling the 'swap' function 
swap(&a, &b);   

printf("\n\nValues after call the swapfunction");
printf("\nValue of A: %d", a);
printf("\nValue of B: %d", b);


getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter first value: 4
Enter second value: 7

Values before call the swap function
Value of A: 4
Value of B: 7

Values after call the swap function
Value of A: 7
Value of B: 4

No comments:

Post a Comment