Write a C program to demonstrate call by value and call by reference.

Call By Value & Call By Reference:
void callByValue(int, int);
void callByReference(int *, int *);
 
int main()
{
    int x=30, y=40;
    clrscr();
    printf("Value of x = %d and y = %d.\n", x,y);
 
    callByValue(x, y);
    printf("\nCall By Value function call...\n");
    printf("Value of x = %d and y = %d.\n", x,y);
 
    callByReference(&x, &y);
    printf("\nCall By Reference function call...\n");
    printf("Value of x = %d and y = %d.\n", x,y);
 
    getch();
    return 0;
}
 
void callByValue(int x, int y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
}
 
void callByReference(int *x, int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

OUTPUT:
      Values of x = 30 and and y=40.
      Call By Value function call... 
      Values of x = 30 and and y=40.
      Call By Reference function call...
      Values of x = 40 and and y=30.

No comments:

Post a Comment