Explain Call by Reference in C language ?

Call by value

  • In call by reference, original value is modified because we pass reference (address).
  • Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.

Sample Code

#include<stdio.h>
void change(int *num)
{
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(&x);
printf("After function call x=%d \n", x);
return 0;
}

Output

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,