Ticker

6/recent/ticker-posts

C Language (Call by reference in C)

CALL BY VALUE IN C

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.





CALL BY VALUE EXAMPLE IN C
#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
















Post a Comment

0 Comments