Tuesday, 22 August 2023

Call by Value and Call by reference.

Call by Value and Call by Reference in C

Call by Value and Call by Reference in C

#include 

void CallByReference (int *x, int *y)
{
  int temp;
  temp = *x;
  *x = *y;
  *y = temp;
  return;
}

void CallByValue (int x, int y)
{
  int temp;
  temp = x; 
  x = y;    
  y = temp;
  return;
}

int main ()
{
  int a = 100;
  int b = 200;
  printf ("------Call by Value--------\n");
  printf ("Before swap, value of a: %d\n", a);
  printf ("Before swap, value of b: %d\n", b);
  CallByValue (a, b);
  printf ("After swap, value of a: %d\n", a);
  printf ("After swap, value of b: %d\n", b);
  printf("---------------------------\n");
  printf ("------Call by Reference-----\n");
  printf ("Before swap, value of a: %d\n", a);
  printf ("Before swap, value of b: %d\n", b);
  CallByReference (&a, &b);
  printf ("After swap, value of a: %d\n", a);
  printf ("After swap, value of b: %d\n", b);
  printf("---------------------------\n");
  return 0;
}

1 comment:

  1. Output:

    ------Call by Value--------

    Before swap, value of a : 100

    Before swap, value of b : 200

    After swap, value of a : 100

    After swap, value of b : 200

    ---------------------------

    ------Call by Reference-----

    Before swap, value of a : 100

    Before swap, value of b : 200

    After swap, value of a : 200

    After swap, value of b : 100

    ReplyDelete

Interactive Report: Introduction to the Internet of Things (IoT) ...

Popular Posts